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 的语法,我没有看到任何区别,因此我不明白为什么这两个不起作用。因为我远不是 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 回答