66

I'm using HTML5 form validation to validate phone numbers from India.

Phone numbers from India are 10 digits long, and start with 7, 8, or 9.

For example:

  1. 7878787878
  2. 9898989898
  3. 8678678878

These phone numbers are valid, but

  1. 1212121212
  2. 3434545464
  3. 6545432322

are invalid.

Suggest a pattern that can detect valid India phone numbers.

So far, my pattern is [0-9]{10}, but it doesn't check if the first digit is 7, 8, or 9.

Zim
  • 388
  • 1
  • 4
  • 15
Manwal
  • 23,450
  • 12
  • 63
  • 93
  • 2
    [This answer](http://stackoverflow.com/q/27000681/1256925) should help you validate phone numbers for all over the world. It also includes validation for other types of input. – Joeytje50 Nov 18 '14 at 18:12
  • 2
    Why not use min="7000000000" and max="9999999999" ? – Farhad Mar 11 '18 at 05:42
  • starting from 6 mobile numbers are also valid [see here](https://en.wikipedia.org/wiki/Mobile_telephone_numbering_in_India) – Garvit Jain Feb 21 '20 at 10:04

7 Answers7

100

How about

<input type="text" pattern="[789][0-9]{9}">
JamesPlayer
  • 1,834
  • 2
  • 15
  • 21
  • 3
    However, I would recommend adding both the start and end anchors due to browsers being inconsistent with following the HTML5 specification about anchoring - Chrome and Firefox have both only anchored at the start at some point in the past. – SEoF Feb 06 '17 at 17:42
57

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.

itsmikem
  • 2,118
  • 2
  • 26
  • 31
  • why /d is used in pattern. what is meaning? – Manwal Oct 26 '13 at 20:42
  • 4
    \d means any digit and the {9} is 9 instances of any digit. – itsmikem Oct 26 '13 at 20:42
  • congo dude its works i big relief. but why you have placed / at staring and ending of pattern. – Manwal Oct 26 '13 at 20:44
  • the / at the beginning and end of the regular expression denotes the beginning and end of the regular expression. So, in your code, it's this: var myRegEx = /(7|8|9)\d{9}/; – itsmikem Oct 26 '13 at 20:45
  • 5
    you may also want to add a terminator to the end of the regular expression otherwise it may accept other characters after the nine digits so you should have `/(7|8|9)\d{9}$/` – Austyns May 22 '17 at 10:53
  • its failed on 789999999999999 . number count more than 10+ – Selva Ganapathi Jul 13 '18 at 14:58
  • For it to work with more numbers, you'd have to write it like this: /(7|8|9)\d{9,}/ The comma after the 9 means 9 or more. – itsmikem Jul 13 '18 at 16:54
12

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

Shailendr singh
  • 686
  • 6
  • 19
  • I think a better solution for this is `/^[+]91(9|8|7)\d{9}$/` which is more appropriate for ECMA6. We don't really need the \d while checking for the first digit. – ShrikarC Dec 30 '19 at 21:45
12

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)

sam
  • 1,800
  • 1
  • 25
  • 47
thejustv
  • 2,009
  • 26
  • 42
6

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.

Ben Thomas
  • 3,180
  • 2
  • 20
  • 38
manish1706
  • 1,571
  • 24
  • 22
5

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.

Darshan Gada
  • 583
  • 5
  • 9
0

enter image description here

^[789]\d{9,9}$

  • Minimum digits 10
  • Maximum digits 10
  • number starts with 7,8,9
bunny
  • 1,316
  • 2
  • 13
  • 22