Search code examples
angularjsregexng-pattern

Pattern that restricts user to enter special characters for first character


I have a requirement for an input field that should allow numbers, alphabets, and special characters like #,$,%,^ etc. But with only one condition the first character that I enter in the text box should not be a special character.

Example:

  1. @Test123 --Invalid character
  2. Test@123 --Valid character
  3. T@est123% --Valid character

I tried this

ng-pattern="/^([a-zA-Z0-9]+)([a-zA-Z0-9 &()_+@&\-=\\|,.\/?\s]+)$/"

But not working.


Solution

  • Try this one

    ng-pattern="/^[a-zA-Z0-9]+[^]*$/"

    [a-zA-Z0-9]+ means one of alphabet or number and

    [^]* means everything