Validation on text on alphabets enter

2019-03-12 18:29发布

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

5条回答
smile是对你的礼貌
2楼-- · 2019-03-12 18:33

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楼-- · 2019-03-12 18:42

Depending on what you want:

Alphabets and blankspace:

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

Alphabets no blankspace:

ng-pattern="/^[a-zA-Z]*$/"
查看更多
\"骚年 ilove
4楼-- · 2019-03-12 18:44

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楼-- · 2019-03-12 18:52

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]/" ...
查看更多
淡お忘
6楼-- · 2019-03-12 18:54

add this to your input

ng-pattern="/^[a-zA-Z]$/"
查看更多
登录 后发表回答