16

Any one use AngulerJS Validation on TextBox. So that only enter alphabets.

Greg
  • 9,068
  • 6
  • 49
  • 91

5 Answers5

20

Depending on what you want:

Alphabets and blankspace:

ng-pattern="/^[a-zA-Z\s]*$/"

Alphabets no blankspace:

ng-pattern="/^[a-zA-Z]*$/"
4

I guess all the other answers using ng-pattern are correct but just to mention, the use of ng-model is compulsory with ng-pattern.

The complete input can be:

 <input type="text" ng-pattern ="/^[a-zA-Z\s]*$/" ng-model="Name" />

This will accept the alphabets and the spaces only.

Prateek
  • 1,065
  • 8
  • 6
0

There is a directive called ng-pattern it allows to use regular expressions to indicate which are the allowed values.

It can be used like this:

<input ng-pattern="/[a-zA-Z]/" ...
Dalorzo
  • 19,834
  • 7
  • 55
  • 102
0

add this to your input

ng-pattern="/^[a-zA-Z]$/"
Brian
  • 903
  • 1
  • 7
  • 13
0

If you don't want to use ng-pattern for whateever reason, you can also add validating functions to the ng-modal-controller that is set up on your textbox, specifically to the formatters and parsers arrays. A good description of how to do this is here: http://www.benlesh.com/2012/12/angular-js-custom-validation-via.html. Another alternative is to use a published directive to achieve the same purpose, I haven't used this one myself but it seems legit: http://www.brentmckendrick.com/code/xtform/

Derek
  • 722
  • 1
  • 6
  • 16