Search code examples
regexalphanumeric

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


Solution

  • ^\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.