David Addoteye
David Addoteye

Reputation: 1641

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}/"

Upvotes: 0

Views: 944

Answers (3)

Shushanth Pallegar
Shushanth Pallegar

Reputation: 2862

this should match only one digit

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

Upvotes: 1

Brent Washburne
Brent Washburne

Reputation: 13138

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>

Upvotes: 0

Richard Hamilton
Richard Hamilton

Reputation: 26434

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.

Upvotes: 1

Related Questions