Minimum 3 characters, all small case, can use maximum of 2 numbers, no special characters allowed. I tried using ^[a-zA-Z0-9]*$ but I'm unable to limit the numbers used
Can someone help me.
CodePudding user response:
You could use a negative lookahead assertion to rule out more than 3 digits:
/^(?!(?:.*\d ){3,})[a-z0-9]{3,}$/
Here is an explanation of the pattern:
^
from the start of the string(?!(?:.*\d ){3,})
assert that 3 or more digits do NOT occur[a-z0-9]{3,}
then match 3 or more lowercase letters or digits$
end of the string
Here is a working demo.
CodePudding user response:
You could use check if there are at least 3 allowed characters, and then match 0, 1 or 2 digits.
^(?=[A-Za-z\d]{3})[A-Za-z]*(?:\d[A-Za-z]*){0,2}$
Explanation
^
Start of string(?=[A-Za-z\d]{3})
Positive lookahead, assert 3 allowed chars[A-Za-z]*
Match optional chars A-Za-z(?:\d[A-Za-z]*){0,2}
Repeat 0-2 times matching a single digit and optional chars A-Za-z$
End of string
See the matches on regex101.