Validate a number between 10 and 11 characters in length

19,016

Solution 1

Well to do what you're asking:

if (stripped.length != 10 && stripped.length != 11) {
    alert("Please enter a valid US phone number.")
    return false
} 

Still, you may consider using regular expressions to validate phone numbers as they can easily validate a large number of conditions.

Borrowing from RegEx Buddy (which I strongly recommend as it will help you dissect and understand regular expressions):

var phoneRegex = /\(?\b[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}\b/;​​

var phone = '3334445555';

if(!phone.match(phoneRegex)){
    alert('please enter a valid phone number');
}
​

Matches 3334445555, 333.444.5555, 333-444-5555, 333 444 5555, (333) 444 5555 and all combinations thereof.

EDIT

@pawel makes a good point. The best way to validate is to strip out all characters other than digits first. Then ensure you have the correct number of digits, then save that stripped, uniform number to the server. The regex is ideal only for finding valid phone numbers that are hiding in a bigger block of text. It really isn't a smart choice for this...oops :)

Solution 2

The simple answer is to add a check in your if:

if (!(stripped.length == 10 || stripped.length == 11)) { ... }

Solution 3

Well you probably really want either 10 characters, or a 1, followed by 10 characters.

if (!(stripped.length === 10 || stripped.length === 11 && stripped.charAt(0) === "1")) {
  // invalid...
}
Share:
19,016
Montana Flynn
Author by

Montana Flynn

I love good design and having fun.

Updated on June 28, 2022

Comments

  • Montana Flynn
    Montana Flynn almost 2 years

    I am using javascript (and PHP) to validate a simple form with a phone number field. I have it working fine checking that the field has only 10 characters, but I really want to check if the field has between 10 and 11 characters.

    Reason being, some people type numbers like so: 1 555 555 5555 and some people do this 555 555 5555.

    Here is what I have that works for checking if the length is 10:

            if (!(stripped.length == 10)) {
                alert("Please enter a valid US phone number.")
                return false
            } 
    
  • Montana Flynn
    Montana Flynn about 14 years
    LOL, I didn't refresh the page when I answered my own question. Looks like some people answered it for me!
  • Montana Flynn
    Montana Flynn about 14 years
    Thanks for the awesome suggestion and link to RegEx buddy, very useful stuff.
  • Montana Flynn
    Montana Flynn about 14 years
    Thanks Daniel, I actually figured it out shortly after you posted the answer.
  • Jesper Haug Karsrud
    Jesper Haug Karsrud about 14 years
    Either way, it's bad practice to answer your own question. You should have edited it in to your question that you found a solution to the problem. Remember it next time ;)
  • pawel
    pawel about 14 years
    Formatting conventions do not matter, as you probably don't want to store phone numbers the way peple have entered them, so RegExp validation against many posiible conventions isn't very useful. If I had to use RegExp to validate a phone number I'd rather remove all non-digits, like phoneNumber.replace(/\D/g, ''); and check if it's 10 or 11 characters long. Do the same while inserting these numbers to database and you get: 1. fixed-size Int column (not a hard to determine varchar), and 2. easy way to format phone numbers for display in a uniform way.