ram
ram

Reputation: 2343

Regular expression to avoid special characters and should not start with numbers

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

Answers (3)

vks
vks

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

Regular Jo
Regular Jo

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

anubhava
anubhava

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

Related Questions