regex for alphanumeric, but at least one character

后端 未结 6 1929
遇见更好的自我
遇见更好的自我 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:13

    ^\w*[\p{L}]\w*$
    

    This one's not that hard. The regular expression reads: match a line starting with any number of word characters (letters, numbers, punctuation (which you might not want)), that contains one letter character (that's the [\p{L}] part in the middle), followed by any number of word characters again.

    If you want to exclude punctuation, you'll need a heftier expression:

    ^[\p{L}\p{N}]*[\p{L}][\p{L}\p{N}]*$
    

    And if you don't care about Unicode you can use a boring expression:

    ^[A-Za-z0-9]*[A-Za-z][A-Za-z0-9]*$
    

提交回复
热议问题