custom phone number regex - javascript

I am using regex ^(\+91|[0]?)\d{10}$ for phone number validation. I want below output.
+911234567891 - valid
01234567891 - valid
1234567891 - valid
0123456789 - should be invalid as I want 10 digits after 0.
Please suggest changes in regex pattern
Thanks in advance

Your ^(\+91|[0]?)\d{10}$ pattern matches +91 or an optional 0 and then any 10 digits. That means any 10 digit string will pass the test. You need to make sure 10 digits are allowed after +91 or 0, or make sure the first digit is 1 to 9 and the rest is just 9 digits.
You may use
^(?:(?:\+91|0)\d{10}|[1-9]\d{9})$
See the regex demo.
Details
^ - start of string
(?:(?:\+91|0)\d{10}|[1-9]\d{9}) - 2 alternatives:
(?:\+91|0)\d{10} - +91 or 0 and then any 10 digits
| - or
[1-9]\d{9} - a digit from 1 to 9 and then any 9 digits
$ - end of string.

Say for instance, in php - and javascript flavor you can use a possessive quantifier. Demo here. Code below:
^(\+91|0?+)\d{10}$
The change is replacing [0]? with 0?+. I removed the [...] for the sake of convenience. Then, the ?+ matches one 0 and won't let it go.
Another alternative is to list all the opportunities:
^(\+91\d{10}|0\d{10}|[1-9]\d{9})$
Demo here.

Only mistake in your regex is a misplaced question mark.
^(\+91|0)?\d{10}$
You can remove the square bracket around '0' as it is a single character.

/^([+]{0,1})([0]|[9][1]){0,1}([\d]{10})$/

Related

Reqular expression of 7 digits numbers with optional special characters in between

I need validate a 7 digit number with optional dash in between.
I was able to get if I use below.
^(\d-?\d-?\d-?\d-?\d-?\d-?\d)$
Is there a way to shorten that?
I tried ^(\d+(-?){7})$ but it's not working.
Valid 123-09-23
Valid 12-3092-3
Valid 1-230-9-23
Valid 1234567
Invalid -1237883
Invalid 12345678
InValid 123-45-678
PS: I will be implementing this in my Javascript application.
Repeat the group only (7 times, so you get 7 digits total), and don't repeat the \d as well (else you may match more digits than desired):
^(?:\d-?){7}$
https://regex101.com/r/yLQHWW/1
(Your original pattern is equivalent to: "Match one or more digits, optionally followed by up to 7 - characters".)
Start with a digit and repeat -?\d six times:
^\d(-?\d){6}$
https://regex101.com/r/oTSqri/1

Javascript regular expression validate phone number

I need to validate phone number by regular expression. The phone number should
Start with (9|09|8869|+8869)
Followed by 8 digit [0-9]
I come up with /(09|9|8869|+8869)[0-9]{8}$/g.
I test with +8869900000000 and expect it will not match but actually it passed
Could you help me to address the regex problem? And how do I fix it?
You can use this regex: /^(0?9|\+?8869)\d{8}$/
The group (0?9|+?8869) is for your starting condition where 0 is optional before 9 and + is optional before 8869.
Demo: https://regex101.com/r/1OpYl0/1/
The regex you are looking for is : ^(([0]?9)|([+]?8869))[0-9]{8}$
Note the way round brackets used to determine the conditions. We need to match within any of the 2 subsets and then precede it 8 digits.

How to check a mobile number that shouldn't have duplicated digits with Regex

I want to check if a mobile number has 11 digits, starts with zero and 3rd digit could be 0,1,2,3 and 9 only like 09123456789. I used this pattern ^(09)([01239])\d{8}$ for this purpose, also I wanna check that 4th to 11th digits are not duplicated completely like 09123333333 or 090311111111.
I use this Regex pattern: ^(09)([01239])(?!\2{8,})\d{8}$ to achieve this purpose but unfortunately it doesn't work.
Note that I have to use one Regex pattern exactly.
Could anyone help me please?
You may use
^09[01239]\d(\d)(?!\1{6})\d{6}$
It matches
^ - start of string
09 - a 09 substring
[01239] - a digit from the set
\d - a digit
(\d) - Group 1: a digit
(?!\1{6}) - no same digit as captured into Group 1 is allowed
\d{6} - six digits
$ - end of string.

Javascript regex for extracting certain part of string

I need to extract certain part of Javascript string. I was thinking to do it with regex, but couldn't come up with one which does it correctly.
String can have variable length & can contain all possible characters in all possible combinations.
What I need to extract from it, is 10 adjacent characters, that match one of next two possible combinations:
9 numbers & 1 letter "X" (capital letter "X", not X as variable letter!)
10 numbers
So, if input string is this: "[1X,!?X22;87654321X9]ddee", it should return only "87654321X9".
I hope I've explained it good enough. Thanks in advance!
This Regex will work:
\d{9}X|\d{8}X\d|\d{7}X\d{2}|\d{6}X\d{3}|\d{5}X\d{4}|\d{4}X\d{5}|\d{3}X\d{6}|\d{2}X\d{7}|\d{1}X\d{8}|\d{10}|X\d{9}
As described, It need to match 9 digits and any letter, and the letter can be at any position of the sequence.
\d{9}X # will match 9 digits and a letter in the end
\d{8}X\d # will match 8 digits a lettter then a digit again
...
\d{1}X\d{8} # will match 1 digits a lettter then 8 digits
\{10} # will match 10 digits
Edited to match only X
You can use this much simpler regex:
/(?!\d*X\d*X)[\dX]{10}/
RegEx Breakup:
(?!\d*X\d*X) # negative lookahead to fail the match if there are 2 X ahead
[\dX]{10} # match a digit or X 10 times
Since more than one X is not allowed due to use of negative lookahead, this regex will only allow either 10 digits or ekse 9 digits and a single X.
RegEx Demo
This regex has few advantages over the other answer:
Much simpler regex that is easier to read and maintain
Takes less than half steps to complete which can be substantial difference on larger text.

Regex pattern for mobile number validation with extension

I have seperate regex validations for my requirement but struggling to combine them in one.
I am validation mobile numbers with country code or starting with 00 and also if they contain extension number(2-5 digits) seperated by #
Following is the example of valid Number :
+919986040933
00919986040933
+919986040933#12
+919986040933#123
+919986040933#1234
+919986040933#12345
I have following regex to validate the above:
var phoneRegexWithPlus = "^((\\+)|(00))[0-9]{10,14}$";
var phoneRegexWithZero = "^((\\+)|(00))[0-9]{9,12}$";
var phoneRegexExtension = "^[0-9]{2,5}$";
Currently i am checking whether number contains #,if yes then split it and match number and extension part seperately where extension is comething after hash.
My problem is now that i have to create one regex combining the above three,can anyone help me with that as m not good in regex.
Thanks in advance.
I suggest this expression:
^\+?(?:00)?\d{12}(?:#\d{2,5})?$
See the regex demo
Expression explanation:
^ - start of string
\+? - an optional plus (as ? matches the + one or zero times)
(?:00)? - an optional 00
\d{12} - Exactly 12 digit string
(?:#\d{2,5})? - an optional (again, ? matches one or zero times) sequence of:
# - a literal hash symbol
\d{2,5} - 2 to 5 digits (your phoneRegexExtension)
$ - end of string.
The phoneRegexWithPlus and phoneRegexWithZero are covered with the first obligatory part \+?(?:00)?\d{12} that matches 12 to 14 digits with an optional plus symbol at the start.
NOTE: The regex is adjusted to the sample input you provided. If it is different, please adjust the limiting quantifiers {12} (that can be replaced with, say, {9,14} to match 9 to 14 occurrences of the quantified pattern).

Categories