1) How to regex validate user input to contain any combination of characters from group A and to not contain any characters from another group D?
2) Also check string length is between 2 and 255
In other words, for all string characters: A AND NOT D.
I receive two groups of characters (whitelist and blacklist) from a server and need to validate user input based on those. I cannot affect the design and must live with it. I also must use regex because of other design restrictions.
Here's what I've got so far (not working at all):
/^(?![23]+)[0-9]{2,255}$/
23 would be the blacklist of characters for simplicity
0-9 would be the whitelist of characters for simplicity
Some examples:
3014567890 --> fail, 3 is present
0145678902 --> fail, 2 is present
0123456789 --> fail, 2 and 3 are present
014567890 --> ok
88774411489 --> ok
5 --> fail, not enough characters
1abc --> fail, abc illegal chars
ab1c --> fail, abc illegal chars
abc1 --> fail, abc illegal chars
Thanks!
You're nearly there, the lookahead assertion needs some work:
/^(?!.*[23])[0-9]{2,255}$/
That way, the regex within the negative lookahead matches if there is (at least) one 2
or 3
anywhere in the string (ie, after any number of characters ( .*
)), causing the assertion to fail.
In this (apparently) simplified example, you could of course just have used /^[014-9]{2,255}$/
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.