Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查输入中是否包含int_Javascript_Html_Error Handling_Int - Fatal编程技术网

Javascript 检查输入中是否包含int

Javascript 检查输入中是否包含int,javascript,html,error-handling,int,Javascript,Html,Error Handling,Int,我正在处理一个HTML页面,似乎不知道如何检查用户输入是否包含整数。我在for循环isNaN中尝试了parseInt、userInput.charAt(I)。似乎什么都没用 这是我的HTML <label for="author" id="author">Favorite Book Author: </label> <input type="text" name="author" id="authorText"> 在for循环中,然后在if语句中比较x。但似

我正在处理一个HTML页面,似乎不知道如何检查用户输入是否包含整数。我在for循环isNaN中尝试了parseInt、userInput.charAt(I)。似乎什么都没用

这是我的HTML

<label for="author" id="author">Favorite Book Author: </label>
<input type="text" name="author" id="authorText">
在for循环中,然后在if语句中比较x。但似乎什么都不管用

加法器函数如下所示

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);
}

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

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

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

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

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

函数checkAuthor(){
var userInput=$(“authorText”).value;
for(var i=0;i}
您应该检查字符代码是否介于48和57之间

函数checkAuthor(){
var userInput=$(“authorText”).value;
for(var i=0;i}
jQuery和JavaScript不一样。
不能在返回的$()中使用
值。相反,使用
val()
并使用
#
将ID用作选择器。 使用
[0-9]
检查数字是否存在

函数checkAuthor(){
var userInput=$(“#authorText”).val();//添加#以获取ID和val()
var结果=/[0-9]+/.test(用户输入);
结果?警报(“假”):无效(0);
console.clear();
console.log(!result);
返回!结果;
}

最喜欢的书作者:

jQuery和JavaScript不一样。
不能在返回的$()中使用
值。相反,使用
val()
并使用
#
将ID用作选择器。 使用
[0-9]
检查数字是否存在

函数checkAuthor(){
var userInput=$(“#authorText”).val();//添加#以获取ID和val()
var结果=/[0-9]+/.test(用户输入);
结果?警报(“假”):无效(0);
console.clear();
console.log(!result);
返回!结果;
}

最喜欢的书作者:

由于您只寻找一个数字,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);
}

由于您只寻找一个数字,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);
}


你应该使用一个常规的表达你在阅读输入值时有一个打字错误。使用$(“#authorText”).value;这是可能的,但你只关心数字吗?其他非字母输入可能与/\d/.test(userInput)重复;若要检查输入是否包含数字,应使用常规表达式。在读取输入值时,您有一个打字错误。使用$(“#authorText”).value;这是可能的,但你只关心数字吗?其他非字母输入可能与/\d/.test(userInput)重复;要检查输入是否包含数字出于某种原因,即使我在HTMLCA中输入了一个没有整数的字符串,它仍然会输入if语句。您可以在使用正则表达式的地方共享完整的代码块吗?什么是正则表达式?从未听说过正则表达式这个词,就像我在示例代码中使用的一样/[^a-zA-Z]+//出于某种原因,即使我在HTMLC中输入了一个没有整数的字符串,它仍然会输入if语句。您可以在使用正则表达式的地方共享完整的代码块吗?什么是正则表达式?从未听说过正则表达式这个词,就像我在示例代码中使用的那样/[^a-zA-Z]+/.value??你在jQuery中的实际意思是什么?同样定义
addError
谢谢.val(),我在编辑我的消息后看到了这些评论。我假设加法器函数是在代码中的其他地方定义的,与问题无关。我添加了加法器函数,因为它现在在问题中。值??你在jQuery中的实际意思是什么?同样定义
addError
谢谢.val(),我在编辑我的消息后看到了这些评论。我假设addError函数是在代码中的其他地方定义的,与问题无关。我添加了addError函数,因为它现在在问题中。RegEx用于这样的场景。那么为什么要在代码中加重载呢?我在尝试此操作时遇到一个错误“Uncaught ReferenceError:charCodeAt未定义”。确定我只是修复了函数,请再试一次。他只是想检查字符串是否包含int值,这意味着他不仅允许使用abcy,还允许使用其他字符。检查两个答案,一个是我的,另一个是@gyre的。您可以看到,regex的使用是为了在这样的场景中使用。那么为什么要在代码中加重载呢?我在尝试此操作时遇到一个错误“Uncaught ReferenceError:charCodeAt未定义”。确定我只是修复了函数,请再试一次。他只是想检查字符串是否包含int值,这意味着他不仅允许使用abcy,还允许使用其他字符。检查两个答案,一个是我的,另一个是@gyre的。您可以看到regex的用法