0

私はクレジット カード フォームを持っており、JS を使用してオンザフライでデータ検証を実行しています。何らかの理由で、有効期限の月と年のフィールドでデータ検証が機能しません。Uncaught SyntaxError: Unexpected identifier

以下は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 の構文を他のフィールドと比較して確認しましたが、違いが見られないため、これら 2 つが機能しない理由がわかりません。私はJSの専門家にはほど遠いので、ここで問題を実行すると思いました(明らかな間違いがあるかもしれません...)

jstlメッセージも確認しましたが、そこからエラーは発生していません。

問題の原因がわかりましたか?

4

1 に答える 1

0

修正された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','Error Exp');"></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','Error Year');"></input></span>
  <p class="error" id="err_expiration"></p>
  <br />

また:

<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 />

「validateCreditCardExpYear」関数に何を渡したいかによって異なります。

validateCreditCardExpYear 関数の最後のパラメーターに、正しくない形式の文字列を渡しています。

于 2013-07-23T14:26:26.380 に答える