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 訊息替換 | 豬窩

We're a gaggle of volunteers and starting a new scheme in our community.
Your website offered us with valuable information to work on. You've done an impressive job and our whole community will be thankful to you.

作者 nikkiferree.hatenablog.com @ 09:35, 2017-05-19

javascritp 訊息替換 | 豬窩

Good day very cool website!! Guy .. Excellent .. Wonderful ..
I'll bookmark your website and take the feeds additionally?
I am glad to seek out a lot of helpful info right here within the put up, we
want develop extra techniques on this regard, thank you for
sharing. . . . . .

作者 foot pain after surgery @ 23:05, 2017-06-21

javascritp 訊息替換 | 豬窩

Inspiring story there. What occurred after? Take care!

javascritp 訊息替換 | 豬窩

First off I want to say terrific blog! I had a quick question which I'd like to ask if you
do not mind. I was curious to find out how you center yourself and clear your mind prior to
writing. I've had difficulty clearing my mind in getting my ideas out.
I do enjoy writing but it just seems like the first 10 to 15 minutes are lost just trying to figure out how to
begin. Any recommendations or tips? Many thanks!

作者 foot pain causes @ 04:32, 2017-06-27

javascritp 訊息替換 | 豬窩

Hi there i am kavin, its my first time to commenting anywhere,
when i read this paragraph i thought i could also create comment due to this good
piece of writing.

作者 foot pain bottom @ 15:40, 2017-07-02
發表迴響
 authimage