1

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.

Shaik Subhan
  • 284
  • 1
  • 10
forgottofly
  • 2,729
  • 11
  • 51
  • 93
  • You did not add the `@` and `%` symbols to the second character class. Maybe a better expression will look like [`/^[a-zA-Z0-9][A-Za-z0-9_!-,.-\/:-@[-\`{-~-]*$/`](https://regex101.com/r/0O6koU/1) – Wiktor Stribiżew Jan 06 '17 at 09:55
  • 1
    BTW, if you do not care what chars there will be after the first one, use `ng-pattern="/^[a-zA-Z0-9]/"`. Do not forget to add `ng-trim="false"` if you do not want to allow leading/trailing whitespace in the input field. – Wiktor Stribiżew Jan 06 '17 at 10:01

1 Answers1

2

Try this one

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

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

[^]* means everything

Mr_Perfect
  • 8,254
  • 11
  • 35
  • 62
  • Just out of curiousity: why not `^[\w&[^_]].*$`? Of course if `_` does not have to be excluded than you don't need the intersection. – D. Kovács Jan 06 '17 at 13:14