0

I am validating a web page via a javascript function using regular expressions. Here is my variable:

var RE_phone = /^[\d\+]{3,4}([\- ]?)\d{1,3}([\- ]?)\d{3}([\- ]?)\d{4}$/;

I need to validate both national and international numbers. So far it is working correctly for international numbers.

Here is an example of a national number that I would like to be able to accept as a valid number: 04 4784897 or 044784897.

Can I please have some help to modify my regular expression variable to accept national phone numbers in the above format?

4
  • Check this link may be help you stackoverflow.com/questions/123559/…
    – Hkachhia
    Apr 16, 2012 at 6:09
  • Thanks for the link. However, none of those examples on that page validates a number of the following format: 04 4784897 or 044784897. Apr 16, 2012 at 6:19
  • 1
    There is no way you can validate all possible phone numbers via regex. You are very likely to create an annoyance rather than something helpful, and you're wasting your time trying. Just let people enter whatever they want.
    – Tomalak
    Apr 16, 2012 at 6:26
  • As I say, the above variable is working perfectly, except for the 04 4784897 or 044784897 instances. It should be pretty easy to modify my variable to accept these numbers, I am just having a little trouble with it. Apr 16, 2012 at 6:36

1 Answer 1

0

Wouldn't it be easier to first remove all non digits from the equation and then test for length (in case of your 04 4784897 number should be 9) and/or other criteria?

'04 9876543'.replace(/[^\d]/g,'').length === 9;
//other examples
'050 4312345'.replace(/[^\d]/g,'').length == 10; //=>true
var intnr = '+31 0610-752-772'.replace(/[^\d]/g,'');
intnr.length >= 10; //=>true
//etc...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.