【问题标题】:I need this code to check for digits only我需要此代码仅检查数字
【发布时间】:2015-06-19 06:59:01
【问题描述】:
var tel = "123457890";
if (tel.length != 10) {
    console.log("Sorry, incorrect format.");
} else {
    var areaCode = tel.substring(0,3);
    var prefix = tel.substring(3,6);
    var suffix = tel.substring(6,10);
    console.log("(" + areaCode + ") " + prefix + "-" + suffix);
}

这一切都对我有用,除了我不能让它只检查数字。当我通过建议对其进行故障排除并通过http://repl.it/languages/JavaScript 运行它时,我遇到了错误消息。

即当我输入一个带有 10 个数字的字符串的“w”时,我希望它返回“对不起,格式不正确”,就好像我输入了错误数量的数字等一样。

【问题讨论】:

标签: javascript


【解决方案1】:

你可以使用正则表达式:

var tel = "123457890";
if (!tel.match(/^\d{10}$/)) {
    console.log("Sorry, incorrect format.");
} else {
    var areaCode = tel.substring(0,3);
    var prefix = tel.substring(3,6);
    var suffix = tel.substring(6,10);
    console.log("(" + areaCode + ") " + prefix + "-" + suffix);
}

【讨论】:

    【解决方案2】:

    这是一个小提琴:

    http://jsfiddle.net/e1qq659g/

    这是一个正则表达式模式:

    http://regexr.com/3b77b

    var regularExpression = /\d{10}/g;
    (tel.match(regularExpression)) ? alert("All digits!") : alert("Not Digits!");
    

    这会将表达式与变量 tel 中的字符串匹配 - 如果匹配,则会提醒“所有数字” - 如果不匹配,则会提醒“不是数字!”

    【讨论】:

      【解决方案3】:

      这行得通。它删除所有字母并检查长度是否改变

      var tel = "1234567890";
      if (tel.length !== 10 || tel.length !== parseInt(tel).toString().length) {
          document.write("Sorry, incorrect format.");
      } else {
          var areaCode = tel.substring(0,3);
          var prefix = tel.substring(3,6);
          var suffix = tel.substring(6,10);
          document.write("(" + areaCode + ") " + prefix + "-" + suffix);
      }

      【讨论】:

      • 恐怕这行不通。特别是,它允许浮点数(其中带有“.”)
      【解决方案4】:

      您可以使用RegExp 来检查它是否只包含数字并提取每个部分:

      var tel = document.getElementById("tel");
      var result = document.getElementById("result");
      
      tel.oninput = function(e){
        //var match = tel.value.match(/^(\d{3})(\d{3})(\d{4})$/); // Basic RegExp
        var match = tel.value
          .replace(/\s/g, '') // Removing spaces before RegExp makes it simplier
          .match(/^(\d{3}|\(\d{3}\))(\d{3})\-?(\d{4})$/);
        
        if(match == null) {
          result.innerHTML = "Sorry, incorrect format.";
          return false;
        }
        
        // parseInt is optional
        var areaCode = parseInt(match[1].replace(/[\(\)]/g,'')); // Just need this with the second RegExp
        var prefix = parseInt(match[2]);
        var subfix = parseInt(match[3]); 
        
        result.innerHTML = "Valida input: (" + areaCode + ") " + prefix + "-" + subfix + "\n"
          + "\n\tArea Code: " + areaCode
          + "\n\tPrefix: " + prefix
          + "\n\tSubfix: " + subfix
      };
      <input type="text" id="tel" placeholder="Enter a valid phone number"/>
      
      <pre id="result"></pre>

      这样您可以更改RegExp 以使其也匹配格式化输入,例如(123)456-7890(123) 456-7890(123) 456 - 7890(123)4567890...这就是当前代码的作用。

      如果您评论当前的 RegExp 并取消评论第一个,它将按照您的要求工作,只接受 10 位输入。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-05
        • 2011-09-22
        • 2011-01-13
        • 1970-01-01
        • 2010-11-04
        相关资源
        最近更新 更多