Page 1 of 1

regex for phone number

Posted: Mon Jul 09, 2012 3:14 am
by global_erp_solution
Rules:
1. must start with number
2. sum of numeric must be between 7-12 inclusive
3. other valid non-numeric characters include space (" ") and hyphens ("-")
4. no more than one consecutive valid non-numeric characters allowed ("1 2-3" is allowed, "1-2" is allowed, but "1--2" is not, "1- 2" is also not allowed)

right now my regex is this:
\\d([- ]?\\d[- ]?){6,11}

but it passed the test 123--4567. What's wrong? thanks

Re: regex for phone number

Posted: Mon Jul 09, 2012 5:04 am
by requinix
Would it not be a lot easier to just strip out all non-numeric characters and validate the length? Then format it however you want.

Re: regex for phone number

Posted: Mon Jul 09, 2012 2:18 pm
by tr0gd0rr
You might need to run a regex for 3 and a separate regex for 4.

Re: regex for phone number

Posted: Wed Aug 22, 2012 10:08 am
by thewebhostingdir
Try following regex :

((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}