Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for alphanumeric, but at least one letter

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).

like image 201
mrblah Avatar asked Jun 27 '09 02:06

mrblah


People also ask

What does ?= * Mean in regex?

?= 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).

How do you check if a string contains at least one character?

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.

How do you write alphanumeric in regex?

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]+$".

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.


1 Answers

^\d*[a-zA-Z][a-zA-Z0-9]*$ 

Basically this means:

  • Zero or more ASCII digits;
  • One alphabetic ASCII character;
  • Zero or more alphanumeric ASCII characters.

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.

like image 69
cletus Avatar answered Sep 17 '22 10:09

cletus