Javascript regular expression validate phone number - javascript

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.

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

custom phone number regex

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})$/

Javascript Regular Expression to match several criteria

How can I write a javascript regular expression that will match the following criteria -
Must contain 8 - 15 characters
Combination of letters (UpperCase OR LowerCase) and numbers (Special characters are allowed, but NOT mandatory)
Not more than 2 repeating characters
Thanks in advance!
I have tried the following but seems not to be working -
/^(?!.*([A-Za-z0-9_#./#&+-])\1{2})(?=.*\d){8,15}$/
You can use this regex:
/^(?!.*?(.)\1{2})(?=\D*\d)(?=[^a-zA-Z]*[a-zA-Z]).{8,15}$/gm
RegEx Demo
This will enforce these rules:
Length between 8 to 15
At least one Upper/Lower case letter
A Digit
Not more than 2 repeats

Regular expression to validate textbox length

I want a regular expression to validate an ASP textbox field with the minimum length of 11 characters and in the middle of the string should be a "-" sign. The sample string is: "0000-011111". I want to validate the textbox to make sure user enters a minimum of 10 numbers with "-" sign after 4 digits using regular expressions. Please help me.
Thank you.
Use
\d{4}-\d{6}
\d represents a digit, - is a literal dash and the number in the curly brackets force the preceeding token to be present the given number of times.
^\d{4}-\d{6,}$
You should use also ^ at the beginning and $ at the end to ensure that there is nothing before and after your string that you don't want to have. Also important is the {6,} so it will match at least 6 digits, without , it will match exactly 6 digits. If you want set a maximum of digits you can specify after the ,, e.g. {6,20}.

Javascript REGEX

I need a javascript REGEX to check that the length of the string is 9 characters. Starts with 'A' or 'a' and is followed by 8 digits.
Axxxxxxxx or axxxxxxxx
/^[aA][0-9]{8}$/ or /^[aA]\d{8}$/
Also makes sure the x's are digits :)
This should do it:
/^[aA]\d{8}$/
or
/^a\d{8}$/i
This is probably what you want.
/^([aA]\d{8})$/
The carot character means the regex must start searching from the beginning of the string, and the dollar character means the regex must finish searching at the end of the string. When they are used together it means the string must be searched from start to end.
The square brackets are used to specific a character or a range of allow characters. The slash and d means to search any digit character. The brackets at the end specify a static quantity that applies to the previous test definition. A range of quantities can be used by specifing a minimum value immediately followed by a comma immediately followed by a maximum value.
did you mean this?
/^[aA]\d{8}/
or did you mean 9 chars ?
/^[aA]\d{8}/
or did you mean A + 8 equal chars ?
/[aA](.)\1{7}/

Categories