【问题标题】:Checking if input has a int in it Javascript检查输入是否有 int Javascript
【发布时间】:2017-07-19 00:05:21
【问题描述】:

我正在处理一个 HTML 页面,但似乎无法弄清楚如何检查用户输入中是否包含整数。我在 for 循环 isNaN 中尝试了 parseInt、userInput.charAt(i)。似乎没有任何效果。

这是我的 HTML

<label for="author" id="author">Favorite Book Author: </label>
<input type="text" name="author" id="authorText">

这是我的 JavaScript

function checkAuthor(){
    var userInput = $("authorText").value;
    for(var i = 0; i < userInput.length; i++)
    {
        if(userInput.charAt(i) <= 0 || userInput.charAt(i) > 0)
        {
            addError("There is a number in 'Favorite Book Author' input");
            return false;
        }
        else
            return true;
    }
}

我需要查看用户是否输入了任何整数。如果输入是“Mark5”、“Ma5rk”、“5”,我需要它来捕捉那个 5 并显示错误。无论如何,这会起作用吗?我有一种感觉 .charAt 不起作用,因为它将整数识别为字符串,但我尝试对每个单曲进行 parseInt

var x = parseInt(userInput.charAt(i));

在 for 循环中,然后在 if 语句中比较 x。但似乎没有任何效果

addError函数是这样的

function addError(text){
    var mylist = window.document.createElement("li");
    var myText = window.document.createTextNode(text);
    mylist.appendChild(myText);
    window.document.getElementByTagName("ul")[0].appendChild(mylist);
}

【问题讨论】:

  • 你应该使用正则表达式
  • 您在读取输入值时出现拼写错误。使用 $("#authorText").value;
  • 这是可能的,但你只关心数字吗?存在其他非字母输入
  • /\d/.test(userInput);检查输入是否包含数字

标签: javascript html error-handling int


【解决方案1】:

您应该检查字符代码是否在 48 和 57 之间

function checkAuthor(){
    var userInput = $("authorText").value;
    for(var i = 0; i < userInput.length; i++)
    {
        var c = userInput.charAt(i).charCodeAt(0);
        if(c <= 57 && c >= 48)
        {
            addError("There is a number in 'Favorite Book Author' input");
            return false;
        }
           
    }
     return true;

}

【讨论】:

  • RegEx 旨在用于此类场景。那你为什么要在代码中重载呢?
  • 我在尝试此操作时收到错误“Uncaught ReferenceError: charCodeAt is not defined”
  • 好的,我只是修复了功能,请重试
  • 他只是想检查字符串是否包含int值,这意味着他允许其余字符不仅是abc
  • 是的。检查 2 个答案,一个来自我,一个来自 @gyre。可以看到正则表达式的使用
【解决方案2】:

您可以使用正则表达式来搜索。

var userInput = $("authorText").value;
if (userInput.search(/[^a-zA-Z]+/)) {
  addError("There is a number in 'Favorite Book Author' input");
return false.
}

【讨论】:

  • 由于某种原因,即使我在 HTML 中输入了一个没有整数的字符串,它仍然会进入 if 语句
  • 你能分享你用过正则表达式的完整代码块吗?
  • 什么是正则表达式?没听说过这个词
  • 正则表达式,就像我在示例代码中使用的那样 /[^a-zA-Z]+/
【解决方案3】:

jQuery 和 JavaScript 不一样。
您不能在返回的 $() 中使用 value。而是使用val() 并使用# 将ID 用作选择器。 使用[0-9] 来检查号码是否存在。

function checkAuthor(){
    var userInput = $("#authorText").val(); // add # to get ID and val()
    var result = /[0-9]+/.test(userInput);
    result?alert("false"):void(0);
    console.clear();
    console.log(!result);
    return !result;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="author" id="author">Favorite Book Author: </label>
<input type="text" name="author" id="authorText" onblur="checkAuthor();" />

【讨论】:

    【解决方案4】:

    由于您在寻找单个数字,因此 RejExp 测试可能是最简单/最干净的方法。

    jQuery

    function checkAuthor(){
        var userInput = $("#authorText").val();
    
        // check whether any char in userInput is a number. yes: true. no: false
        if(/[0-9]/.test(userInput)) {
            addError("There is a number in 'Favorite Book Author' input");
            return false;
        } else
            return true;
        }
    }
    
    function addError(text){
        var mylist = window.document.createElement("li");
        var myText = window.document.createTextNode(text);
        mylist.appendChild(myText);
        window.document.getElementByTagName("ul")[0].appendChild(mylist);
    }
    

    原版JS

    function checkAuthor(){
        var userInput = document.getElementById('#authorText').value;
    
        // check whether any char in userInput is a number. yes: true. no: false
        if(/[0-9]/.test(userInput)) {
            addError("There is a number in 'Favorite Book Author' input");
            return false;
        } else
            return true;
        }
    }
    
    function addError(text){
        var mylist = window.document.createElement("li");
        var myText = window.document.createTextNode(text);
        mylist.appendChild(myText);
        window.document.getElementByTagName("ul")[0].appendChild(mylist);
    }
    

    【讨论】:

    • .value??你在 jQuery 中的实际意思是什么?
    • 也定义addError
    • 感谢 .val(),我在编辑我的消息后实际上看到了这些 cmets。我假设 addError 函数是在代码的其他地方定义的,并且与问题无关。
    • 我添加了addError函数
    • 我只是把它扔了,因为它现在在问题中。
    猜你喜欢
    • 2018-04-14
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多