chriz
chriz

Reputation: 1345

checking numeric in javascript

I want to show the 'valid' message if the variable value is

 

if(isNaN(num) && num !="" && num.length!=10)
{
    alert("invalid");
}
else
{
    alert("valid");
}

But this code shows 'digits which length is not 10' as valid. But whether it is numeric or not numeric, if its length is not 10 it should be invalid.

Upvotes: 0

Views: 138

Answers (2)

Brian
Brian

Reputation: 5028

Did you mean this:

if(is_nan(num) && num !="" && num.length<10)
{
alert("invalid");
}
else
{
alert("valid");
}

Otherwise if length is <9 or >10, you will get false.

In this case you will alert valid when your num is non-numeric, nun-empty string with length >= 10.

Upvotes: 0

Ruchan
Ruchan

Reputation: 3194

Your Condtion placement is wrong here.

isNaN(num) && num !=""
here, for num=1234,isNaN is false(that means it is number), but the num!="" will give true resulting in Invalid alert. 

Solution replace && with || for OR condtion.

Upvotes: 3

Related Questions