regex for alphanumeric, but at least one character

后端 未结 6 1897
遇见更好的自我
遇见更好的自我 2020-11-27 18:21

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 1 character (i.e. can\'t be ALL numbers)

6条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 19:07

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

提交回复
热议问题