Validation on text on alphabets enter

杀马特。学长 韩版系。学妹 提交于 2019-12-03 08:56:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!