Undesirable result of use of ng-pattern (Angular.J

2020-04-10 01:44发布

问题:

I am using an ng-pattern that with an input field that should accept only Hebrew characters.

I have find out what unicode numbers Hebrew chars are.

This is my pattern:

$scope.onlyHebrewPattern = /[\u05D0-\u05F3]+/g;

And my form input:

<input tabindex=1 type="text" ng-disabled="disableButtons" name="firstname" ng-minlength="2" ng-maxlength="15" ng-model="register.firstName" placeholder="first name" ng-pattern="onlyHebrewPattern" required title="please enter your name">

Now for most inputs that pattern will work and will not populate the $scope.firstname with wrong results like: "abcd".

But there are inputs like: "שדas" that for some reason are being accepted by the pattern.

I believe the problem relies with the pattern definition. Something must be wrong but I am sure that u05D0-u05F3 is really that range that I need in my pattern. So what is my problem here?

回答1:

Try this:

$scope.onlyHebrewPattern = /^[\u05D0-\u05F3]+$/g;

Yours matches any string with a Hebrew character.