Ms7 Search
Google 搜尋 翻譯
calendar
« 九月 2024 »
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30            
近期文章
文章分類
網站連結
Syndicate

« | »

javascritp 訊息替換

2012-02-09 @ 19:54 in PHP, JavaScript, Sql

在一個網頁程序中用 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 訊息替換 | 豬窩

Sweet blog! I found it while browsing 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! Thank you

作者 g @ 18:03, 2020-06-16

javascritp 訊息替換 | 豬窩

For latest news you have to pay a visit the web and on web I found this website as a best site for newest updates.

作者 g @ 04:53, 2020-06-17

javascritp 訊息替換 | 豬窩

This article will help the internet visitors for building up
new blog or even a weblog from start to end.

作者 g @ 19:32, 2020-06-17

javascritp 訊息替換 | 豬窩

Hi there, I do think your site may be having internet browser compatibility issues.
Whenever I look at your website in Safari, it looks fine however
when opening in IE, it's got some overlapping issues.
I simply wanted to give you a quick heads up!
Other than that, wonderful blog!

作者 well g @ 07:11, 2020-06-20

javascritp 訊息替換 | 豬窩

Helpful info. Fortunate me I found your site unintentionally, and I'm surprised why this coincidence didn't came about in advance!
I bookmarked it.

作者 give g @ 10:42, 2020-06-21
發表迴響
 authimage