Javascript神秘Uncaught SyntaxError:意外的标识符

时间:2013-07-23 14:14:00

标签: javascript uncaught-exception

我有一张信用卡表格,我用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消息,但错误并非来自那里。

您是否看到导致此问题的原因?

1 个答案:

答案 0 :(得分: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函数中的最后一个参数。