Reputation: 2343
What regular expression can I use to not allow special characters and make certain it does not begin with numbers.
I'm using /[^a-zA-Z0-9]/
which is filtering out special characters. How can I make sure it does not start with numbers in regex itself.
Also i'm using ng-pattern of angular with my input box.
Upvotes: 0
Views: 10169
Reputation: 67968
/^[a-zA-Z][a-zA-Z0-9]*$/
This should do it.
Try this if you want to have one or two words.
^[a-zA-Z][a-zA-Z0-9]*(?:\s+[a-zA-Z][a-zA-Z0-9]+)?$
Upvotes: 1
Reputation: 5500
This should work for you
^[^\W0-9_][^\W_]+$
If Spaces are permitted
^[^\W0-9_][a-zA-Z0-9\s]+$
If you're not trying to match the whole string, you can remove ^ and $ which match start and end respectively.
Upvotes: 0
Reputation: 784918
You can just use this regex:
/^[a-zA-Z]/
to make sure your input is only starting with a letter (non-digit and non-special character).
Upvotes: 0