简体   繁体   中英

ng-pattern to Validate single digit number in Angularjs

I have been searching this forum and others for angularjs pattern that will validate html input with ONLy single digit.

any number between 0-9 but should not accept any character including.

This is what i have done so far, but still does not work properly.

ng-pattern="/[0-9]{1}/"
ng-pattern="/[0-9{1}]/"
ng-pattern="/\d{1}/"

You need to use regex for starting and stopping at that digit

ng-pattern="/\A\d\z/"

Or alternatively

ng-pattern="/^\d$/"

This will validate only 1 digit.

这应该只匹配一位数字

 ng-pattern = /^[\d]$/; 

Why not use a select tag?

<select ng-model="model.id" convert-to-number>
  <option value="0">Zero</option>
  <option value="1">One</option>
  <option value="2">Two</option>
</select>

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