Search code examples
c#model-validation

Regex to allow only alphanumerics with at least one character and no empty space


I keep trying everything from the search result (google) but without success till now. For my username input I want only to allow alphanumerics, without empty spaces and no tabs. The username can have a combination of letters and numbers, but must have at least one character. Can someone please share a regex able to do that, and with a bit explanation if some changes are required to be made later on.

Thank you in advance.

edit: examples what should be allowed:"a012", "2342a", "anyalpha", "abc2xyz". what should not be allowed: "1234123", " ace12", "any space", "space2 ", " space3"


Solution

  • Try Regex: ^(?=\w*[A-Za-z])\w*$

    Demo