In my ASP.NET page, I have an input box that has to have the following validation on it:
Must be alphanumeric, with at least one letter (i.e. can't be ALL numbers).
?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).
To check if a string contains at least one letter using regex, you can use the [a-zA-Z] regular expression sequence in JavaScript. The [a-zA-Z] sequence is to match all the small letters from a-z and also the capital letters from A-Z . This should be inside a square bracket to define it as a range.
For checking if a string consists only of alphanumerics using module regular expression or regex, we can call the re. match(regex, string) using the regex: "^[a-zA-Z0-9]+$".
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.
^\d*[a-zA-Z][a-zA-Z0-9]*$
Basically this means:
Try a few tests and you'll see this'll pass any alphanumeric ASCII string where at least one non-numeric ASCII character is required.
The key to this is the \d*
at the front. Without it the regex gets much more awkward to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With