Any one use AngulerJS Validation on TextBox. So that only enter alphabets.
5 Answers
Depending on what you want:
Alphabets and blankspace:
ng-pattern="/^[a-zA-Z\s]*$/"
Alphabets no blankspace:
ng-pattern="/^[a-zA-Z]*$/"
- 439
- 2
- 6
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.
- 1,065
- 8
- 6
-
I want accents in the vowels (i.e. áÁéÉíÍóÓúÚ). How can I do it? @Prateek – Carmoreno Jan 03 '19 at 15:40
-
[UPDATE] this regex working for me: `/^[a-zA-ZáéíóúÁÉÍÓÚ\s]*$/` – Carmoreno Jan 03 '19 at 15:48
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]/" ...
- 19,834
- 7
- 55
- 102
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/
- 722
- 1
- 6
- 16