0

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

  • not numeric with length of 10
  • an empty string ("")

 

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.

11
  • length is a property valid for string. What do you mean by length here? The number of digits ? Commented Jul 2, 2013 at 7:04
  • If am nt wrong you want lenth not must be greater then 10 Commented Jul 2, 2013 at 7:05
  • number of characters of text box value.. whether it is numeric or alphanumeric Commented Jul 2, 2013 at 7:07
  • if you just want to check for the length of string not equal to 10, irrespective of whether it is a number or not, then you just need one check and that is num.length!=10. Commented Jul 2, 2013 at 7:10
  • @satinder singh length should not be 10 an it should not be numeric. Commented Jul 2, 2013 at 7:13

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

REPLACE your && with || How did you miss that
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.