0

Basically, I'm doing this:

var phone = $("#phone").val();

Then I want to check if:

1) Phone contains only numbers

2) Phone contains at least 10 digits

But the problem is, from my experience, using $("something").val() returns it as a string so isNaN() fails. But I don't want use parseInt() either since it will change strings into numbers as well and then isNaN() will always pass.

Thoughts?

2 Answers 2

3

You could use a regular expression like this one:

if (phone.match(/^\d{10,}$/)) {
    // the input contains at least 10 digits
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks !poskigfo;lksgf;lkg;lf'dkg;l;skfd grable to make 15 chars
1

Like @Andrew said you can use reg ex to do the check like this.

var regExp = new RegExp("^"+pattern+"$","");
return regExp.test(value);

Place the above in a function and pass the patern into it to make it re-useable.

Replace pattern with the one that matches your requirements. The one from @Andrew looks like it'll work.

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.