Regular expression for alphanumeric with atleast one digit - javascript

I am trying to match an alphanumeric string with at least one digit.
The second condition is that it's minimum length should be 3.
For example, the following strings should match
111
12345
ABCD1
123A
11AA11
And the following should not match
ABCD
AB
12
1A
I have got myself to a point where I can get the first condition right.
That is, having minimum one digit:
([a-zA-z0-9]*[0-9]+[a-zA-z0-9]*)
But I don't have any idea to specify a minimum length. If I try using {3},
it will require minimum of 3 numbers.

Try using a positive lookahead to ascertain that there's at least one digit, and use {3,} to indicate that it should match at least 3 characters:
/^(?=.*\d)[a-z\d]{3,}$/i

You can use lookahead to assure your expression contains a digit, and then match the minimum-three-chars:
/^(?=.*?\d)[a-zA-Z0-9]{3,}$/

Related

Regex match valid Phone Number

I'm quite new to regex, and not sure what I'm doing wrong exactly.
I'm looking for a regex that match the following number format:
Matching requirements:
Must start with either 0 or 3
Must be between 7 to 11 digits
Must not allow ascending digits. e.g. 0123456789, 01234567
Must not allow repeated digits. e.g. 011111111, 3333333333, 0000000000
This is what I came up with:
^(?=(^[0,3]{1}))(?!.*(\d)\1{3,})(?!^(?:0(?=1|$))?(?:1(?=2|$))?(?:2(?=3|$))?(?:3(?=4|$))?(?:4(?=5|$))?(?:5(?=6|$))?(?:6(?=7|$))?(?:7(?=8|$))?(?:8(?=9|$))?9?$).{7,11}$
The above regex fails the No. (4) condition. Not sure why though.
Any help would be appreciated.
Thanks
A few notes about the pattern that you tried
You can omit the {1} and the comma in [0,3]
In the lookahead (?!.*(\d)\1{3,}) the (\d) is the second capturing group because this (?=(^[0,3]{1})) contains the first capturing group so it should be \2 instead of \1
In the lookahead, you can omit the comma in {3,}
In the match itself you use .{7,11} where the dot would match any character except a newline. You could use \d instead to match only digits
You pattern might look like
^(?=(^[03]))(?!.*(\d)\2{3})(?!^(?:0(?=1|$))?(?:1(?=2|$))?(?:2(?=3|$))?(?:3(?=4|$))?(?:4(?=5|$))?(?:5(?=6|$))?(?:6(?=7|$))?(?:7(?=8|$))?(?:8(?=9|$))?9?$)\d{7,11}$
Regex demo
Or leaving out the first lookahead and move that to the match, changing the quantifier to \d{6,10} and repeating capture group \1 instead of \2
^(?!.*(\d)\1{3})(?!(?:0(?=1|$))?(?:1(?=2|$))?(?:2(?=3|$))?(?:3(?=4|$))?(?:4(?=5|$))?(?:5(?=6|$))?(?:6(?=7|$))?(?:7(?=8|$))?(?:8(?=9|$))?9?$)[03]\d{6,10}$
Regex demo
Edit
Based on the comments, the string not having 4 ascending digits:
^(?!.*(\d)\1{3})[03](?!\d*(?:0123|1234|2345|3456|4567|5678|6789))\d{6,10}$
Regex demo
A solution for a JS flavor of PCRE would be
/^[03](?!123456(7(8(9|$)|$)|$))(?!(?<d>.)\k<d>+$)[0-9]{6,10}$/
Explanations
^[03] starts at the beginning of the string, then reads either 0 or 3
(?!123456(7(8(9|$)|$)|$)) makes sure that, after this first char, there is no sequence (if a sequence can be read, then the negative lookahead fails
(?!(?<d>.)\k<d>+$) is another negative lookahead : it ensures that the first char read (flagged d) is not repeated again and again until end of string
[0-9]{6,10}$/ finally reads 6 to 10 digits (first one already read)
A few tests:
"0123456789: No match"
"01234567: No match"
"01234568: No match"
"011111111: No match"
"33333333: No match"
"333333233 is valid"
"042157891023 is valid"
"019856: No match"
"0123451245 is valid"

4 or more of the same consecutive letters

Can anyone tell me what I can add to my existing RegEx expression so that 4 or more of the same letter consecutively is invalid? This is what I have so far:
(^[A-Za-z]{1})([A-Za-z\-\'\s]{0,})([A-Za-z]{1}$)
It meets all but 1 of my requirements so far which are:
Any alphabetic character
Single spaces but not as first or last character
Hyphens but not as the first or last character
Single quotes but not as the first or last character
No more than 3 consecutive characters the same, irrespective of case
At least 2 characters long, if present
Some examples:
James - valid
Sarah Jayne - valid
Michellle - valid
O'Brian - valid
Holly-Rose - valid
Eeeeric - invalid
Jo--anne - invalid
Based on your description, edits and comment, you can probably use this regex in Javascript:
/^(?=.{2})(?!.*([a-z])\1{3})[a-z]+(?:[' -][a-z]+)*$/gmi
RegEx Demo
There are 2 lookaheads:
(?=.{2}) - Positive lookahead to ensure there are at least 2 characters in input
(?!.*([a-z])\1{3}) Negative lookahead to ensure we don't allow 4 repeats of alphabets.

Javascript RegExp non sequential characters

I have this rule
var reg = new RegExp('[a-z]{3}');
Which means it is allowed to use characters between a-z and at least 3 occurrences.
So, I am wondering if there is a way to match this rule with non sequential characters.
In other words,
"abc" => valid
"aaa" => not valid
Thank you!
Here is a working regex for exactly 3 (or N) characters, if the number is not fixed it gets more complicated:
^([a-z])(?!\1{2})[a-z]{2}$
1 2 3 4 5 6 7 8
Explanation:
^ matches the beginning of the string
([a-z]) match one of the accepted characters and save it (group 1)
(?!...) negative lookahead, what is in those brackets is not accepted
\1 reference to the first group (first character here)
{2} repeated exactly twice
[a-z] the accepted characters
{2} repeated exactly twice
$ matches the end of the string
Link here (I added the gm modifiers, so that several expressions can be tested.)
Try to use the excluding lookahead (?![a-z]{3}), it will not match 3 equal characters in sequence.

Regex - At least 1 number, 1 letter, 1 special character and at least 3 characters

I wanna test my own regex in order to understand how can i use it and create my custom regex.
I tried this one :
^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_]){3}$
(?=.*\d) => at least 1 number.
(?=.*[a-zA-Z]) => at least 1 letter.
(?=.*[\W_])=> at least 1 special character.
{3} => at least 3 characters.
Unfortunately, it doesn't work, but i want at least 1 number, 1 letter and 1 special character, and at least 3 characters in my input. When the user types those 3 types of characters, my message disappears because the regex is correct ^^
Sorry for my bad english, I can give you more details if you want and thank you for the help :)
Have a nice day :)
The problem is that your {3} quantifier applies to your last look-ahead, which is nonsensical : it means that at the start of the text, you must match 3 times the look-ahead, which is a given if it matches once since lookaround are 0-width matches.
You could use the following :
^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_]).{3,}$
which specifies, aside from your existing look-aheads, that at least 3 characters must be matched.
If you just test the string, it would also be enough to match
^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_]).{3}
without an end anchor : you match 3 characters, and stop matching what follows since your requisites are met.
If you want 1 digit, 1 alpha, 1 special character, those are already at least 3 characters.
^(?=\D*\d)(?=.*?[a-zA-Z]).*[\W_].*$
Here's a demo at regex101. Or for only matching:
^(?=\D*\d)(?=.*?[a-zA-Z]).*[\W_]
(?=\D*\d) The first lookahead requires one digit (preceded by any \D non-digits).
(?=.*?[a-zA-Z]) second lookahead requires one alpha (preceded by any characters).
.*[\W_] matching until one special character.
All together requires at least 3 different characters: 1 digit, 1 alpha, 1 special.

Regex which accepts alphanumerics only, except for one hyphen in the middle

I am trying to construct a regular expression which accepts alphanumerics only ([a-zA-Z0-9]), except for a single hyphen (-) in the middle of the string, with a minimum of 9 characters and a maximum of 20 characters.
I have verified the following expression, which accepts a hyphen in the middle.
/^[a-zA-Z0-9]+\-?[a-zA-Z0-9]+$/
How can I set the minimum 9 and maximum 20 characters for the above regex? I have already used quantifiers + and ? in the above expression.
How would I apply {9,20} to the above expression? Are there any other suggestions for the expression?
/^[a-zA-Z0-9]+\-?[a-zA-Z0-9]+$/
can be simplified to
/^[a-z0-9]+(?:-[a-z0-9]+)?$/i
since if there is no dash then you don't need to look for more letters after it, and you can use the i flag to match case-insensitively and avoid having to reiterate both lower-case and upper-case letters.
Then split your problem into two cases:
9-20 alpha numerics
10-21 characters, all of which are alpha numerics except one dash
You can check the second using a positive lookahead like
/^(?=.{10,21}$)/i
to check the number of characters without consuming them.
Combining these together gives you
/^(?:[a-z0-9]{9,20}|(?=.{10,21}$)[a-z0-9]+-[a-z0-9]+)$/i
You can do this provided you don't want - to be present exactly in middle
/^(?=[^-]+-?[^-]+$)[a-zA-Z\d-]{9,20}$/
[^-] matches any character that is not -

Categories

Resources