2

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.

shaaa
  • 227
  • 1
  • 7
  • 22

3 Answers3

2

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • along with the provided validation (with the initial character that is a letter from Latin alphabet, a digit, or an underscore) i need to allow some special characters in between or end other than /?\%*:|"<.> . Appreciate your help. – shaaa Jan 11 '17 at 10:16
  • i tried with the below regex ^\w[^\<.>?*:\\"\/|%\ ]*$. Please let me know whetner this is correct way or any changes to be made. – shaaa Jan 11 '17 at 11:35
  • 1
    @shaaa This looks right, all you need is to make sure that all your special characters are properly escaped. – Sergey Kalinichenko Jan 11 '17 at 11:59
  • There is a problem in the above Regex. when am using this regex if i give space and write another word it is not accepting. Here is the link. https://regex101.com/r/tQQOAF/3. Any help on this? – shaaa Jan 11 '17 at 16:50
  • @shaaa Regex does not accept strings with spaces, because your regex wants complete words, `^` to `$`, and prohibits spaces (the space before `]` is on the exclusion list). – Sergey Kalinichenko Jan 11 '17 at 16:55
1

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".

Bohemian
  • 412,405
  • 93
  • 575
  • 722
-1

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

Kiran Kumar
  • 1,033
  • 7
  • 20