简体   繁体   中英

Ng-pattern for both negative and positive number

I wanted to restrict the text field to either negative or positive numbers. How can I validate this?

If I enter a positive number, there is no need to enter a '+' sign and if I enter a negative number I need to enter a '-' sign first. The text field should not allow letters.

How is it done? Thank you in advance.

You should add an ng-pattern directive on the html element https://docs.angularjs.org/api/ng/directive/ngPattern

and the regex should be:

"/-?[0-9]+/"

<input ng-pattern="/^-?[0-9]+$/" type="text" ng-model="myModel">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM