Sajith
Sajith

Reputation: 2852

ng-pattern not working properly, matching every string

I need to validate a password field with pattern matching. I used ng-pattern for that. But it is not working properly.

See this pluker link

The pattern I gave here is, the text should contain at least 8 characters with symbols, alphabets and digits. But when I enter any string the pattern match become true.

See the following code

<body ng-controller="MainCtrl">
    <form name="form">
      <input type="text" name="password" ng-model="user.password" ng-pattern="/(^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[\W]).*$)/">
      <input type="submit">
      {{form.password.$error}}
    </form>
</body>

What am I missing here?

Upvotes: 0

Views: 1421

Answers (1)

Nikos Paraskevopoulos
Nikos Paraskevopoulos

Reputation: 40318

This seems to be working. The expression {{form.password.$error}} becomes true if the field is invalid. The empty field does not get validated.

So entering aaaaa (5 letters) makes the field invalid. Adding 1@ (digit+symbol) is still invalid due to length. Adding anything makes it valid (>= 8 characters with letters, digits, symbols → the display says false).

Upvotes: 1

Related Questions