检查文本框值是数字还是字符串 [英] Check whether a textbox value is numeric or string
问题描述
我想检查文本框是包含数值还是使用字符串javascript
i使用此。
i want to check whether a textbox contains a numeric value or a string using javascript i used this.
var query=getquerystring()
if(isNaN(query))
{
alert("query is a string");
}
else{
alert("query is numeric");
}
其中getquerystring()是一个函数
where getquerystring() is a function as
function getquerystring() {
var query=document.getElementById("queryString").value;
return query;
}
但是当我在文本框中输入数字1234时仍然获得查询是一个字符串。
请告诉我我的代码有什么问题,并建议我解决一下。
but when i enter a number 1234 in textbox still getting query is a string. please tell me what is wrong in my code, and suggest me some solution.
推荐答案
更新: 回顾我自己的答案,我意识到了它的问题。您无法与 NaN
进行比较,您需要使用 isNaN
功能:
Update: Looking back at my own answer, I realise the problem with it. You can't compare with NaN
, you need to use isNaN
function:
var query=getquerystring();
if(isNaN(parseFloat(query))
{
alert("query is a string");
}
else{
alert("query is numeric");
}
或者,您可以使用正则表达式模式匹配看看字符串是否匹配它应该是什么。
Or, alternatively, you can use regex pattern matching to see if the string matches what it should.
这篇关于检查文本框值是数字还是字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!