I have a problem with my matches. I have a field that that SHOULD accept a value that may contain any combination of alphanumeric and special characters. I have a code that works fine when the input is a 1) combination of alphanumeric and special characters, and 2) alphanumeric. But if the input contains a combination of either 1) number and special characters ONLY without alphabet or 2) alphabet and special characters ONLY without number, I got an error on my matches. What I wanted to do is to create a matches that best works on the following input combinations:
1) alphabet+number
2) alphabet+special character
3) alphabet+number+special character
4) number+special character
This is my code:
matches: /^(?=.*[0-9]+.*)(?=.*[a-zA-Z]+.*)[0-9a-zA-Z_\[\]\\\^\$\.\|\?\*\+\(\)~!@#%&-=]{8,}$/
I need your suggestions!
Thanks!
It’s because your positive lookahead requires both a
[0-9]to be present and a[a-zA-Z]to be present.Try reducing this to one single lookahead, which just specifies that at least one of
[0-9]and[a-zA-Z]be present:Furthermore, you can improve on the efficiency of the regex by modifying the
.*in your lookahead to be[^0-9a-zA-Z]*: that way the lookahead matches the first alphanumeric it finds and then stops, preventing needless scanning: