Check whether a textbox value is numeric or string [duplicate]
Possible Duplicate:
Is there a (built-in) way in JavaScript to check if a string is a valid number?
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");
}
where getquerystring() is a function as
function getquerystring() {
var query=document.getElementById("queryString").value;
return query;
}
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.
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.
精彩评论