Akxe Akxe - 1 year ago 123
AngularJS Question

ng-valid-pattern for empty string while regExp.test("")==false

I have

<input type="url">
, but angularJS think that the pattern is valid even if it is empty. Everything is in the demo bellow.


Answer Source

Add the attribute data-ng-required="true"

See demo here: http://plnkr.co/edit/4wjiCNzs1OxgQomUxue5?p=preview


So the current version of angular (1.2.15, 1.3.beta) will NOT run the ng-validation regex if there is no data in the field (or an empty string). So I can see that the ng-validation-pattern class is still there after clearing the existing string like you mentioned in your comment. (I'm not sure if this is a bug or is working as designed)

For your use case, what you can do is create a rule to match when both the ng-valid-pattern and the ng-valid class are on the form.


I updated the plunker to show this change: http://plnkr.co/edit/4wjiCNzs1OxgQomUxue5?p=preview

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download