0

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

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

this should match only one digit

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

2 Comments

Thank you, however it is not validating properly.
"should match only one digit"; what do you suspect the + is doing in your regex? ;)
0

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>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.