【问题标题】:Javascript mysterious Uncaught SyntaxError: Unexpected identifierJavascript 神秘 Uncaught SyntaxError: Unexpected identifier
【发布时间】:2014-05-18 04:01:04
【问题描述】:

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

您知道导致问题的原因吗?

【问题讨论】:

  • 你应该使用正则表达式。
  • 读取生成的 HTML 源代码。
  • 很明显,但感谢您指出:我仔细查看了生成的 HTML,错误消息是“date d'expiration non valide”,所以我猜 'of d'过期导致问题
  • @user2177336:我会删除我的答案,因为它有点偏离标记。 (非常相似,但不完全相同。)可能最好在上面发布您的评论作为答案,然后在 SO 允许时(约 48 小时内)接受它。
  • 感谢您对这些人的帮助!

标签: javascript uncaught-exception


【解决方案1】:

更正的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 函数中的最后一个参数传递了格式不正确的字符串。

【讨论】:

    猜你喜欢
    • 2017-10-13
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多