HTML Java Form input field Restrict to only allow international uk numbers e.g +447655123456

Viewed 14

Im trying to find an example that would force a user to have to input +447 as the first four characters and then the remaining 9 have to be only numbers.

so if someone wanting to type 07655 123456 it would not allow. so unless you first character is a + second 4 third 4 fourth 7 then the remaining is 9 numbers.

I can guess this can be done with a prefix and a restriction to numbers for the remaining 9 but from a user and ease of use they dont get it and want to type it all.

Any Advise please just a simple html form with a input field.

1 Answers

HTML5 has new input type as tel which you can straight away use within input. Here, as you are not looking for any brackets () or - between digits so below is what I am suggesting as a validation. If you don't enter correct number, validation message appears on your screen.

<!DOCTYPE html>
<html>
<head>

<form>
  <label for="choose">Enter your phone number</label>
  <input type='tel' pattern='[\+]447\d{9}' title='Required Phone Number (Format: +447999999999)'/>
  <button>Submit</button>
</form>
</html>

Related