Javascript Regular Expression to match several criteria - javascript

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

Related

RegExp for an identifier

I am trying to write a regular expression for an ID which comes in the following formats:
7_b4718152-d9ed-4724-b3fe-e8dc9f12458a
b4718152-d9ed-4724-b3fe-e8dc9f12458a
[a_][b]-[c]-[d]-[e]-[f]
a - optional 0-3 digits followed by an underscore if there's at least
a digit (if there is underscore is required)
b - 8 alphanumeric characters
c - 4 alphanumeric characters
d - 4 alphanumeric characters
e - 4 alphanumeric characters
f - 12 alphanumeric characters
I have came up with this regexp but I would appreciate any guidance and/or corrections. I am also not too sure how to handle the optional underscore in the first segment if there are no digits up front.
/([a-zA-Z0-9]{0,3}_[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12})+/g
Your regex looks good. To optionally match the first 3 digits with an underscore, you can wrap that group with ()?. Also you can force the presence of a digit before the underscore by using {1,3} instead of {0,3}.
Unless you expect that multiple identifiers are following each other without space and should be matched as one, you can drop the last + (for multiple matches on the same line, you already have the g option).
The final regex is ([a-zA-Z0-9]{1,3}_)?[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}
See here for a complete example.
If you also do not need to capture the individual 4-alphanumeric groups, you can simplify your regex into:
([a-zA-Z0-9]{1,3}_)?[a-zA-Z0-9]{8}-([a-zA-Z0-9]{4}-){3}[a-zA-Z0-9]{12}
See here for an example.

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.

javascript Regex to have atmost 5 characters in password

I am trying to implement maximum case in JavaScript for password validation.
I have a scenario where in password user should enter maximum 5 characters (a-z,A-Z) and password length having no restriction
Passsword length no limit
except (a-z,A-Z) ,there is no limtation
charcter(a-z,A-Z) will have atmost 5 in password .Not more than that.Sequence doesnot matter.
I tried /[^A-Z,a-z]*(?:[A-Z,a-z][^A-Z,a-z]*){0,5}/
But it is not working.
Kindly help
Did you even search? It is obviously, that this is a duplicate.
See
Regex javascript for Minimum 8 characters,at least one number and one special character, maximum 32 characters
Password REGEX with min 6 chars, at least one letter and one number and may contain special characters
JavaScript regex for alphanumeric string with length of 3-5 chars
You can just count how many characters you have in the password like so:
if(password.match(/[a-zA-Z]/g).length > 5){ /* reject */ }
You can use the below regex for password validation.
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%]).{5,20})
It validates :
Must contain ! digit [0-9]
One lowercase character [a-z]
One uppercase character [A-Z]
One symbol [##$%]
Length of password must be between [5-20].
Break it down and solve it step by step
Alphabet only - define your character set
/[A-Za-z]/
Maximum length of 5 - use a quantifier
/[A-Za-z]{0,5}/
Nothing else is allowed - wrap it with ^ and $
/^[A-Za-z]{0,5}$/

Regex for six characters with at least one digit

I am looking for a regex with at least 6 characters (no limit) including at least one digit. No spaces allowed.
I have this this regex:
^(?=.*\d).{4,8}$
However, I don't want to limit to 8 characters.
a regex with at least 6 characters (no limit) including at least one digit. no spaces allowed.
^(?=\D*\d)\S{6,}$
Or
^(?=\D*\d)[^ ]{6,}$
See demo
^ Start of string
(?=\D*\d) - Must be 1 digit (the lookahead is based on the principle of contrast)
\S{6,} - 6 or more non-whitespaces
OR
[^ ]{6,} - 6 or more characters other than literal normal space
To enable the regex to match more than 6 characters, you only need to adjust the quantifier. See more about limiting quantifiers here.
^(?=.*\d)([\w]{6,})$
I think it should work fine

Categories