Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 phone number validation with pattern

People also ask

How do you add a validation to a phone number in HTML?

The <input type="tel"> defines a field for entering a telephone number. Note: Browsers that do not support "tel" fall back to being a standard "text" input. Tip: Always add the <label> tag for best accessibility practices!

Which syntax validate a phone number in the client side?

Validate a Phone Number Using a JavaScript Regex and HTMLfunction validatePhoneNumber(input_str) { var re = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/; return re. test(input_str); } function validateForm(event) { var phone = document.


How about

<input type="text" pattern="[789][0-9]{9}">

How about this? /(7|8|9)\d{9}/

It starts by either looking for 7 or 8 or 9, and then followed by 9 digits.


Try this code:

<input type="text" name="Phone Number" pattern="[7-9]{1}[0-9]{9}" 
       title="Phone number with 7-9 and remaing 9 digit with 0-9">

This code will inputs only in the following format:

9238726384 (starting with 9 or 8 or 7 and other 9 digit using any number)
8237373746
7383673874

Incorrect format:
2937389471(starting not with 9 or 8 or 7)
32796432796(more than 10 digit)
921543(less than 10 digit)


The regex validation for india should make sure that +91 is used, then make sure that 7, 8,9 is used after +91 and finally followed by 9 digits.

/^+91(7\d|8\d|9\d)\d{9}$/

Your original regex doesn't require a "+" at the front though.

Get the more information from below link
w3schools.com/jsref/jsref_obj_regexp.asp


This code will accept all country code with + sign

<input type="text" pattern="[0-9]{5}[-][0-9]{7}[-][0-9]{1}"/>

Some countries allow a single "0" character instead of "+" and others a double "0" character instead of the "+". Neither are standard.


Improving @JamesPlayer's answer:

Since now in India, a new series of phone numbers started with 6. Use this 👇

<input type="text" pattern="[6789][0-9]{9}">

To show a custom error message, use the title attribute.

<input type="text" pattern="[6789][0-9]{9}" title="Please enter valid phone number">

That's all folks. 🎉