javascritp 訊息替換
在一個網頁程序中用 javascript 判斷輸入資料內容,在 Dreamweaver 內用 行為的 Onclick 檢驗表單,錯誤訊息會用欄位名稱顯示,為了更明確的讓用戶知道錯誤訊息,就將欄位名稱用中文訊息顯示,用 javascript 的 replace 和 json 寫法如下:
下面黑色字部分是 Dreamweaver 產生的,藍色部分是將錯誤訊息直接用中文顯示,紅色部分是我加入的,這樣就會用中文顯示,檔名前後加 - 是因為避免有部份相同欄位名稱一樣,被替換
<script type="text/javascript"> <!-- function MM_validateForm() { //v4.0 var fname_arr={'-_name-':'姓名','-_year-':'出生年','-_month-':'出生月','-_day-':'出生日','-_epasswd-':'修改列印密碼','-_reg_zip-':'戶籍地址郵遞區號','-_reg_addr-':'戶籍地址','-_school_name-':'國中學校','-_school_no-':'學校代碼','-_sup_name-':'監護人姓名','-_sup_rela-':'監護人關系','-_sup_tel2-':'監護人電話','-_cnt_zip-':'通訊處郵遞區號','-_cnt_addr-':'通訊處地址'}; if (document.getElementById){ var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments; for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]); if (val) { nm=val.name; if ((val=val.value)!="") { if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@'); if (p<1 || p==(val.length-1)) errors+='- -'+nm+'- 必須是 e-mail 地址.\n'; } else if (test!='R') { num = parseFloat(val); if (isNaN(val)) errors+='- -'+nm+'- 必須是數字.\n'; if (test.indexOf('inRange') != -1) { p=test.indexOf(':'); min=test.substring(8,p); max=test.substring(p+1); if (num<min || max<num) errors+='- -'+nm+'- 必須是數字在 '+min+' 至 '+max+'.\n'; } } } else if (test.charAt(0) == 'R') errors += '- -'+nm+'- 不能空白.\n'; } } for(var key in fname_arr){ errors = errors.replace( key , fname_arr[key]) ; } if (errors) alert('發生以下錯誤:\n'+errors); document.MM_returnValue = (errors == ''); } } //--> </script>
迴響
javascritp 訊息替換 | 豬窩
My brother suggested I would possibly like this
website. He was once entirely right. This submit truly
made my day. You cann't consider just how much time I had spent
for this information! Thanks!
javascritp 訊息替換 | 豬窩
As the admin of this site is working, no hesitation very quickly it
will be famous, due to its feature contents.
javascritp 訊息替換 | 豬窩
I am very happy to read this. This is the kind of manual that needs to be given and not the random
misinformation that is at the other blogs. Appreciate
your sharing this greatest doc.
javascritp 訊息替換 | 豬窩
Sweet blog! I found it while searching on Yahoo News. Do you have any tips on how to get listed
in Yahoo News? I've been trying for a while but I never
seem to get there! Thanks
javascritp 訊息替換 | 豬窩
This is my first time visit at here and i am actually
happy to read all at single place.