future future - 6 months ago 55
AngularJS Question

ng-pattern Validation not working

I am trying to do validation with angular and Bootstrap but for some reasons ng-pattern validation is not working in Zip Code. Any help will be great in this.

Here's FIDDLE LINK

Not able to paste full code, but here is how I am attempting to use the

ng-pattern
directive:

<input type="text" class="form-control" id="zipCode" name="zipCode"
ng-model="auth.zip" ng-pattern="^(\d{5}-\d{4}|\d{5})$" required="">

Answer

The problem is that when you place the pattern inline instead of a scope variable it expects / around the regex, like a litteral.

Like this:

/^(\d{5}-\d{4}|\d{5})$/

Fiddle

See the input docs, check out the arguments section then ngPattern