3

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
lobengula3rd
  • 1,821
  • 4
  • 26
  • 39
  • Looks like ng-pattern matches the start of a value, not the whole value. See http://stackoverflow.com/questions/18746913/ng-pattern-for-only-numbers-will-accept-chars-like-in-angular-js – bobince Oct 15 '13 at 23:51

1 Answers1

4

Try this:

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

Yours matches any string with a Hebrew character.

TimK
  • 4,635
  • 2
  • 27
  • 27
  • looks like this solved the problem. could you please explain why this works? – lobengula3rd Oct 16 '13 at 00:58
  • The special regex characters ^ and $ mean "only at the start" and "only at the end", respectively. The pattern you had said "match a string that contains (hebrew-char)(one-or-more-times). But "ab(hebrew)" matches that description - it contains one hebrew character. Using (only-at-start)(hebrew)(one-or-more)(only-at-end) forces ALL the characters to be hebrew, since the hebrew characters have to be next to the start and next to the end, and there can't be any non-hebrew characters in the middle. – Austin Hastings Oct 16 '13 at 04:05
  • hey again. i don't know why but this pattern seams to not work as it should. sometimes inputs that are ok are being rejected for some reason. what is the reason for that? and if i want to add the option to put '-' and "'" chars, how will i add them to the regexp pattern? – lobengula3rd Oct 16 '13 at 11:29
  • Can't tell what's wrong if you don't give us some information. How about updating the question with an example that still doesn't work. – TimK Oct 16 '13 at 14:23
  • To add more allowable characters, list them inside the square brackets. "-" should go at the beginning, like this: /^[-'\u05D0-\u05F3]+$/g – TimK Oct 16 '13 at 14:25