Reputation: 11
I'm completely new and have a question. I have an XML parameter form file with field validation like this:
ValidChars="(?=.\*[a-zA-Z0-9@#$%*])" ErrorOnValidationFailure="Entered Invalid Char."
The current expression ((?=.*[a-zA-Z0-9@#$%*]))
can generate an error only if the first char is incorrect. Once I enter a valid character, the validation stops working. Is there a regex that will enable me to identify an invalid character in any part of the string?
Thanks
Upvotes: 1
Views: 387
Reputation: 627190
The point is that (?=.*[a-zA-Z0-9@#$%*])
regex executed once only asserts if the input string contains a character from the specified ranges/sets defined in the character class after 0+ characters (in NFA regex, other than a newline/carriage return/etc. - depends on the regex library).
What you need to use is a regex to check if the whole string consists of your whitelisted characters, so you need a
^[a-zA-Z0-9@#$%*]*$
See the regex demo
The $
might be replaced with \Z
in Python, or \z
in PCRE/.NET regex to disallow trailing newline at the end of the string to be accepted.
If the pattern is used by a method that anchors the pattern by default, you need no ^
, $
, \A
, nor \z
/\Z
.
Upvotes: 1