Phone Number Regular Expression Validation

Howdy

I’ve been searching for a decent phone number regular expression validation and it turns out a lot harder to dig one up than I expected. I’ve found plenty, but it turns out that most of them appear on the surface to be fine, but in reality they don’t actually work, like this one:


//Example, try this out for yourself
var phoneRegEx = /\\(?\\d{3}\\)?[-\\/\\.\\s]?\\d{3}[-\\/\\.\\s]?/;
var string = 'this does not belong here 01 2345 6789';
alert(string.match(phoneRegex));

As you can see, this phone validation routine is no good. So, I decided to come up with my own expression that could, as best as possible handle phone number in Australian format, firstly, but in formats from international numbers wherever possible too. Here’s the kind of phone formats I am thinking of:

Standard Telephone numbers
+61 1 2345 6789
+61 01 2345 6789 (zero entered is not required but enterd by user anyway)
01 2345 6789
01-2345-6789
(01) 2345 6789
(01) 2345-6789
1234 5678
1234-5678
12345678

Mobile Numbers
0123 456 789
0123456789

International Phone Numbers
US Format - +1 (012) 456 7890
US Virgin Islands (four digit international code) +1-340 123 4567

You get the idea. So here’s the expression I’ve come up with…


var phoneRegEx = /^((\\+[1-9]{1,4}[ \\-]*)|(\\([0-9]{2,3}\\)[ \\-]*)|([0-9]{2,4})[ \\-]*)*?[0-9]{3,4}?[ \\-]*[0-9]{3,4}?$/;

It’s far from perfect but it might be a step in the right direction. I’m hoping someone here might be able to provide some input to improve upon this, or even simplify it to make it work better.

One of the main problems with it is that it will allow a number through with more than 8 or 10 digits, which is not a valid phone number, at least that I am aware of. Something like 0123456789876543210 would get through when it shouldn’t.

Hmmm… This is also far from perfect, but it will work for length:

[1]([0-9][^0-9]){8,10}$

:agree:


  1. ^0-9 ↩︎

You’ll save yourself alot of headaches to allow users to select their format from a dropdown & validate according to that. :wink:

Hey guys, thanks for the response…

Joe, I can see your point, and yes it would be a dream to be validating if you knew the exact format of the number that was coming at you.

However, without the intention of being smart here, I think it is development idealism to think we can add one more field to a form than absolutely necessary. Especially when it will make the difference between someone potentially making the decision of actually filling out the form or making the decision that this is all too hard and hitting their back button.

So, while I see the credit in your suggestion and what I am trying to achieve might seem excessive I might continue on for a bit.

The following should accept most international and local formats.

^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$

Nice one.

This is perfect for what I need. Although I do need to have it so that the user may not enter the first zero in their phone number.

So 0208 123 4565 becomes 208 123 4565 - also is it possible to specifiy that they do not enter spaces?

All help greatfully received!

Ashley

Some phone number formats require a leading zero so blocking that will stop their numbers from validating.

the numbers that I need to validate all need to have the first zero removed and also not allow for spaces to be entered…I can’t seem to work out which bit does what in the expression above - could somebody give me some pointers on how to edit this?

Regards
AshleyH