Search code examples
javascriptangularjsregexng-pattern

Angular ng-pattern regex not working


I'm trying to create a validation for an angular input field which accepts only the a-zA-Z0-9 characters and special characters ".”(full stop), "_" (underscore), "@"(at sign)

I can't seem to get the hang of it.

I tried:

ng-pattern="/[a-zA-Z0-9_@.]+/"

requirement:

  • It can contain only alphabetic characters
  • it can contain alphabetic and numeric
  • It can contain alphabetic numeric and mentioned special characters
  • It cannot contain space

Solution

  • The ng-pattern should be as follows:

    ng-pattern="/^[a-zA-Z0-9_@.]+$/" 
    

    See demo here: http://plnkr.co/edit/ElBuuxGilBbC9fdA2n0P?p=preview