Wednesday, 25 May 2016

Mobile or Phone Number validations formats in regular expressions

HTML Patterns:

 1. example : +91 96 5541 6053
pattern="[\+][ 0-9 ]{2,8}[' '][0-9 ]{2}[' '][0-9 ]{4}[' '][0-9 ]{4}";

// where  [\+ 0-9 ' ']  for STD or country code may be used
// [0-9 ]{2}[' '][0-9 ]{4}[' '][0-9 ]{4} for 10 digit number with space

<input type="text" name="phone" required
pattern="[\+][ 0-9 ]{2,8}[' '][0-9 ]{2}[' '][0-9 ]{4}[' '][0-9 ]{4}"
 title="Enter phone number format(+91 92 6545 7412)">

 2. example : 9655416053//only 10 numbers
pattern="[0-9 ]{10}";
 <input type="text" pattern="[0-9 ]{10}" onkeypress='return event.charCode >= 48 && event.charCode <= 57'></input>
//[0-9 ]{10} -for 10 digits
//onkeypress='return event.charCode >= 48 && event.charCode <= 57' - for enter only numbers

3.Minimum 10 or 10+ numbers
pattern="[0-9 ]{10,}";
<input type="text" pattern="[0-9 ]{10,}" onkeypress='return (event.charCode >= 46 && event.charCode <= 57 || event.charCode==8 || event.which==8 || event.keyCode==8 ||event.charCode==118 || event.which==118 || event.keyCode==118 )'></input>

//where event.charCode==8 for back space 118 for paste

No comments:

Post a Comment