问题
Any one use AngulerJS Validation on TextBox. So that only enter alphabets.
回答1:
Depending on what you want:
Alphabets and blankspace:
ng-pattern="/^[a-zA-Z\s]*$/"
Alphabets no blankspace:
ng-pattern="/^[a-zA-Z]*$/"
回答2:
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.
回答3:
add this to your input
ng-pattern="/^[a-zA-Z]$/"
回答4:
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/
回答5:
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]/" ...
来源:https://stackoverflow.com/questions/26257412/validation-on-text-on-alphabets-enter