0
我有信用卡的形式和我與JS動態數據驗證執行。出於某種原因,數據驗證不會爲到期月份和年份裏幹活:Uncaught SyntaxError: Unexpected identifier
神祕的Javascript未捕獲的SyntaxError:意外的標識
貝婁是HTML:
<div class="controls controls-row">
<label class="h2_style"><fmt:message key='payment_exp'/><span class="required">*</span></label>
<input class="span1 noBottomMargin" maxlength='2' type='text' data-encrypted-name='expiration_month' id='credit_card_expiration_month' value='11' onChange="validateCreditCardExpMonth('credit_card_expiration_month','err_expiration',<fmt:message key='forms.card_date'/>);"></input>
<span><input class="span1 noBottomMargin" maxlength='4' type='text' data-encrypted-name='expiration_year' id='credit_card_expiration_year' value='2015' onChange="validateCreditCardExpYear('credit_card_expiration_year','err_expiration',<fmt:message key='forms.card_date'/>);"></input></span>
<p class="error" id="err_expiration"></p>
<br />
而且JS:
function validateCreditCardExpMonth(id,err_id,err){
var err_text=err;
var number = document.getElementById(id).value;
var reg = new RegExp('^(0[1-9]|1[0-2])$', 'i');
if(reg.test(number)){
document.getElementById(id).style.borderColor="green";
document.getElementById(err_id).innerHTML="";
}
else{
document.getElementById(id).style.borderColor="red";
document.getElementById(err_id).innerHTML=err_text;
}
}
function validateCreditCardExpYear(id,err_id,err){
var err_text=err;
var number = document.getElementById(id).value;
var reg = new RegExp('^(201[3-9]|202[0-3])$', 'i');
if(reg.test(number)){
document.getElementById(id).style.borderColor="green";
document.getElementById(err_id).innerHTML="";
}
else{
document.getElementById(id).style.borderColor="red";
document.getElementById(err_id).innerHTML=err_text;
}
}
我檢查了onChange對其他字段的語法,我沒有看到任何區別,因此我不明白爲什麼這兩個不起作用。當我從一個JS專家很遠,我想我在這裏運行的問題(也許還有一個明顯的錯誤...)
我也查了JSTL消息和錯誤不是從那裏來。
你看到是什麼原因造成的問題?
你應該使用正則表達式。 – SLaks
閱讀生成的HTML源代碼。 – SLaks
明顯,但感謝它指向了:我有一個仔細看看生成的HTML和錯誤消息是「日期D'到期的非的Valide」所以我猜D'到期的'導致了問題 – user2177336