0

Brief

Validate a UK Mobile Phone Number field in a HTML Form to take into account the following rules:

  1. Phone Number must begin 07
  2. Phone Number must not exceed 11 Digits
  3. Spaces are allowed (ignored). I've found that Auto-Fill features sometimes save numbers like 0123456789 or 01234 456 789. I'd rather not cause the inconvenience to the user to reformat their auto-fill input.

Valid Input Examples

  • 07234567890
  • 07234 567 895
  • 07 23 45 78 90

Where I am

So far, I've only been able to validate my second point using:

<input id="phonenum" type="tel" pattern="^\d{11}$" required >

Chris
  • 4,672
  • 13
  • 52
  • 93
Craig
  • 139
  • 1
  • 2
  • 12
  • 1
    this is mainly a question of regular expression, I'd use a bit of javascript here as the scenario is more complex, check this https://stackoverflow.com/questions/11518035/regular-expression-for-uk-based-and-only-numeric-phone-number-in-cakephp – moped Nov 16 '17 at 16:11
  • [This is validation for India you have manage ](https://stackoverflow.com/questions/19611599/html5-phone-number-validation-with-pattern) – vishal patel Nov 16 '17 at 16:21
  • I wanted to keep the solution fairly streamlined by sticking with regex. @sirnino appears to have what I needed for this brief. – Craig Nov 16 '17 at 17:08

3 Answers3

1

Hope it helps

 <input class="form-control" pattern="[789][0-9]{9}" type="text">
0
function validatePhoneNumber(){
    var e = document.getElementById('phonenum');
    var phoneNo;
    var firstLetter;
    var secondLetter;
    if(!e){
        return false;
    }

    phoneNo = e.value.replace(/ /g, ''); // remove spaces
    if(phoneNo.length != 11 ){
        return false;
    }

    firstLetter = phoneNo[0];
    secondLetter = phoneNo[1];
    if(firstLetter != "0" || secondLetter != "7"){
        return false;
    }

    return true;
}
rupindr
  • 604
  • 4
  • 14
0

If you want to use regular expressions you can use the following

^(0\W*7\W*(?:\d\W*){9})$
sirnino
  • 437
  • 3
  • 13