-3

Possible Duplicate:
Phone Number Validation Javascript

Hi I am creating validation for a form using javascript. I am wanting to check the telephone number field using if statements to ensure it contains a 4 digit area code then a space followed by a 7 digit number.

Any help would be appreciated.

Thanks

Community
  • 1
  • 1
  • 4
    [*what have you tried?*](http://whathaveyoutried.com/) – zzzzBov Jan 17 '13 at 17:02
  • 4
    *then a space*?! Don't make your users format their phone numbers. Ignore (or rather strip) spaces, parentheses, hyphens etc, then format it yourself. – Quentin Jan 17 '13 at 17:03
  • Use regular expressions: http://blog.stevenlevithan.com/archives/validate-phone-number – Big McLargeHuge Jan 17 '13 at 17:04
  • Phone number structures vary from place to place. If you limit yourself to 11 digit phone numbers then you will upset anyone who tries to use a phone number from another country. It usually isn't a good idea to limit yourself that way. – Quentin Jan 17 '13 at 17:04
  • 3
    Since you are new, StackOverflow is like fishing, if you just want a fish, we have some dead ones on the bank left over from last weeks catch run off now and grab those. If you want to learn HOW to fish, show us what you are doing, we can help perfect your craft. SO has fishing guides, not fishermen. If you want to hire a fisherman to just bring you fresh fish, this is not the place. Please provide your attempts and will will gladly assist in helping you perfect that. – Mark Schultheiss Jan 17 '13 at 17:09
  • Search is your friend: http://stackoverflow.com/search?q=javascript+validate+phone+number – CodingWithSpike Jan 17 '13 at 17:52

1 Answers1

0

So you need to validate something like 1234 1234567

function isValidTel(numberToTest){
    var pattern =/^\d{4} \d{7}$/
    return pattern.test(numberToTest);
}
Varinder Singh
  • 1,570
  • 1
  • 11
  • 25