开发者

Check whether a textbox value is numeric or string [duplicate]

This question already has answers here: 开发者_C百科 Closed 11 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜