I am using the following pattern to validate email
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/igm;
Copied the pattern from this fiddle. (http://jsfiddle.net/jquery4u/5rPmV/) which works wonderful.
I tried implementing the same with AngularJS like this
<input type="text" placeholder="Email" ng-model="Employer.Email" ng-pattern="emailPattern" class="form-control" name="email" required />
<p ng-show="showMessages && registerEmployerForm.email.$error.required" class="text-danger">
Email is required.
</p>
<p ng-show="showMessages && !registerEmployerForm.email.$error.required && registerEmployerForm.email.$error.pattern" class="text-danger">
Email is invalid.
</p>
JavaScript
$scope.showMessages = true;
$scope.Employer = {
"Email": ""
};
$scope.emailPattern = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/igm;
$scope.RegisterEmployer = function(myForm) {
console.log(myForm)
};
My Fiddle: http://jsfiddle.net/codeandcloud/jusoqs88/
The problem is that if I try naveen@naveennaveen.com, the first fiddle passes and the second fiddle fails. My questions
- Why for the same pattern AngularJS behaves differently?
- Is there something with my code?
P.S: I know input type="email"
combined with registerEmployerForm.email.$error.email
is the Angular way to do it. Unfortunately I cannot implement it here as the regex should not pass something like naveen@naveennaveen