Reputation: 135
I would like to have a regular expression that allows only letters, numbers and special characters but does not allow numbers, special characters and white space only. I am using this expression below but it is not working. How can I fix Iit?
"^(?=.*\\d)(?=.*[a-zA-Z]).{4,10}$"
For example::
Abc!@#111 - Accept
qwerty - Accept
111 - dont accept
@#$ - dont accept
Upvotes: 2
Views: 118
Reputation: 626747
In your regex, letters and digits are both required. Thus, it won't match qwerty
.
It seems you just want to make sure there is at least one letter.
If you plan to use server-side validation only, you can use a .NET regex that allows Unicode letter class \P{L}
meaning non-letter:
^(?!\P{L}*$)\S{4,10}$
It can be declared as @"^(?!\P{L}*$).{4,10}$"
.
See demo
If you need to validate strings on the client side or both client and server side, you will need a JS compatible version (but it won't support Unicode):
^(?![^A-Za-z]*$)\S{4,10}$
To check if not only numbres or Special characters are present, we can use a look-ahead (?!\P{L}*$)
that requires the use of at least one letter.
Upvotes: 2