Chris Bolton Chris Bolton - 11 months ago 45
AngularJS Question

ng-pattern not working when limiting to 24 characters with regex

We have the following input field and we want the error message to show when the user has inputted 25 characters. However, it is only showing when the user has put in 26 characters.

Here is the input field.

Input Field with 26 charcters

Here is the RegEx:


I have also tried:

Why is this not working when the user hits 25 characters?
Also, we are using angular
if this helps.


It seems to be working, like it is highlighting input fields when it is not satisfying provided pattern in ng-pattern. Also it gives error after 25 because it starts with 0 to 24 since it makes an field invalid when its you are entering 26

Plunkr Here

As you are just worried about to enter character till 25 length, I'd say Despite of ng-pattern use ng-maxlength attribute by specifying length in it.

<input ng-model="myModel" ng-maxlength="25"/>