Regex to validate that the first letter of a word in input field is not a special character

1.8k views Asked by At

I am using angularjs. I want to use validation for my name field. I am a beginner in regex expressions. I want that the first letter of the name is not a special Character. For E.g My$Repo should be valid and $MyRepo is invalid.

I am using ng-pattern to validate the name field. What regex expression should i use? Appreciate your help.

3

There are 3 answers

5
Sergey Kalinichenko On BEST ANSWER

You can use ^\w.*$, which means "a string of at least one character, with the initial character that is a letter from Latin alphabet, a digit, or an underscore.

Demo.

0
Bohemian On

There are lots of "special" characters, but few "non-special"; I would specify what is allowed:

^[a-zA-Z].*

This means "any Latin letter followed by anything".

To allow letters from any alphabet:

^\p{L}.*

This means "any letter from any alphabet followed by anything".

0
Kiran Kumar On

The ragex for special characters in the beginning is /^[$!@#%^&*(\-)_+=-\][{}|';,./?><:"]/g