-3

I am using jQuery.validate plugin where I need to validate UK phone number with the following rule:

  • It must start with 0
  • It should be 10 or 11 digits with no characters allowed (whitespace allowed and not counted)
  • Sequential or identical numbers (eg. 123456, 555555) ARE NOT ALLOWED
  • Premium phone numbers not permitted (070x, 0845, 07624 etc.)

I have a current code which allows up to 12 digits. I need to fix it to 11.. Another fix which I need is to allow comma in it like the given example.. Currently comma is not allowed. Also I need to start it with 0

 jQuery.validator.addMethod('phoneUK', function (phone_number, element) {
        console.log(element);
        return this.optional(element) || phone_number.length > 9 &&
        phone_number.match(/^(((\+44)? ?(\(0\))? ?)|(0))( ?[0-9]{3,4}){3}$/);
    }, 'Please specify a valid phone number');

Valid Phone No: 079 4135 9087 would be valid - so 10 or 11 digits - MUST start with 0 - can not start with 070 - can not start with 0845 - can not start with 07624 - can not be 10 or 11 same digits

Ammar Khan
  • 2,565
  • 6
  • 35
  • 60
  • I see "I need..." a lot, but not a lot of "I've tried…" – Patrick Q Mar 18 '14 at 17:59
  • Can you please read rule which I am currently looking for. – Ammar Khan Mar 18 '14 at 18:01
  • [This question](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation?rq=1) might help – CDspace Mar 18 '14 at 18:05
  • Bro, if you are not interested to suggest, then please leave it. Others are there to help around. Thanks – Ammar Khan Mar 18 '14 at 18:06
  • Validating sequential isn't really something regex is great at doing. You might want to consider separate validations: One which checks that it is a phone number with regex, the other that the phone number is permissible. – adamdc78 Mar 18 '14 at 18:20
  • Can you please give me an example. I am not good with regex. I am wondering to make it work by any way – Ammar Khan Mar 18 '14 at 18:23
  • What exactly is your definition for "Sequential or identical numbers"? Does `123` fail the test? What about `222`? Do you want to allow whitespace at any position in the string? Or just in those positions where it is commonly found in UK phone numbers? Where do you want to allow commas (and why? I deal with the UK market and have yet to see commas in a phone number). It would be helpful if you provided examples of strings that you want to pass the test, and some that would fail the test. – Patrick Q Mar 18 '14 at 18:29
  • Can you check my question again, I just update – Ammar Khan Mar 18 '14 at 18:38

2 Answers2

0

First of all you must clean up the phone number string given by the user (javascript code) below:

var cleanStr = phoneNumber.replace(/[^+0-9]/gi, "");

Then, validate it:

var isGood = /(^\+440[0-9]+$)|(^0[0-9]+$)/gi.test(cleanStr);

If that passed, then you want to further check for unwanted sequences of numbers. I could not understand how many total digits you want to check for: including the country code, excluding it, etc. etc. but you can work it out from my examples.

scrat.squirrel
  • 3,607
  • 26
  • 31
0

This is about as good as it gets for a "simple" script without better definitions of your rules. Note that there is a chance that some invalid (or at least unused) numbers may pass this test. For example, I'm not sure that 04xxxxxxxx is valid, but it would pass this test, as you didn't specify it as invalid.

jQuery.validator.addMethod('phoneUK', function (phone_number, element) {
        console.log(element);
        return this.optional(element) || phone_number.length > 9 &&
        phone_number != '0123456789' && phone_number != '012345678' &&
        phone_number.replace(/\s|,/g, '').match(/^0(?!70|845|7624)(\d)(?!\1+$)\d{8,9}$/);
    }, 'Please specify a valid phone number');

This is the best that I'm able to explain what's actually happening. Anyone smarter than myself is welcome to chime in…

phone_number != '0123456789' && phone_number != '012345678' // basic sequential checking
phone_number.replace(/\s|,/g, '') // remove any whitespace and commas before running regex
^0      // the string starts with a 0
(?!70|845|7624) // the next characters are NOT 70, 845, or 7624 (you could expand this list if you want)
(\d)   // the next character IS some digit
(?!\1+$)   // but that digit is not the only digit used
\d{8,9}    // followed by 8 or 9 additional digits
$          // end of the string
Patrick Q
  • 6,373
  • 2
  • 25
  • 34