Reputation: 1819
I'm trying to verify that user input in a UserName field is NOT in the format of an email address, using ng-pattern with Angular. The following code seems to produce the results I want, however the page loads with the error message showing until the user begins typing. The error goes away unless the users input matches the email address regex. How can I hide that message on page load and only show it when the regex it matched? Thanks.
<div ng-show="!form.UserName.$error.pattern" class="errorMessage">
Username is not your email address
</div>
<div class=" form-group">
<label class="form-label" for="userName">Username</label>
<input class="form-control" ng-pattern="/^([a-zA-Z0-9])+([a-zA-Z0-9._%+-])+@([a-zA-Z0-9_.-])+\.(([a-zA-Z]){2,6})$/" />
</div>
Upvotes: 0
Views: 568
Reputation: 8652
try to check if the input is "touched"
<div ng-show="form.UserName.$error.pattern && form.UserName.$touched" class="errorMessage">
Username is not your email address
</div>
Upvotes: 1