Is there any downside to using AngularJS built-in email validation, compared to using ng-pattern with email regex?
http://www.w3schools.com/angular/angular_validation.asp
Btw, Is this AngularJS email validation or is it using HTML5 validation?
Is there any downside to using AngularJS built-in email validation, compared to using ng-pattern with email regex?
http://www.w3schools.com/angular/angular_validation.asp
Btw, Is this AngularJS email validation or is it using HTML5 validation?
For a website, you should be OK only checking for an @
as users can always change your frontend. In your case, you're using an HTML5 validation, which will be even better.
From the link you provided:
Use the HTML5 type email to specify that the value must be an e-mail
However, your web back-end can and should do more stringent checking to ensure you actually received a real email address before using it for anything.
There are some differences between HTML5 email validation and angular ng-pattern. Generally it depends on what pattern type you are using. Below is a simple example
1. HTML5
e@e is valid
email with html5 whereas
2. ng-pattern as shown in code
e@e is invalid
email address and you can customize, you can make it valid using different ng-pattern
From above two examples as you know ng-pattern is effective when you need a strict email from the user
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.emailAddr = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/;
$scope.submitBtn = function() {
debugger;
console.log($scope.htmlemailModel, $scope.emailForm.htmlemail.$valid);
console.log($scope.angemailModel, $scope.emailForm.angemail.$valid);
console.log($scope.emailForm.angemail.$viewValue, $scope.emailForm.angemail.$valid); // this line is just to show you the value even it is invalid
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
<form name="emailForm" novalidate>
HTML: <input type="email" name="htmlemail" ng-model="htmlemailModel" required/> <br /> <br />
Angular: <input type="text" name="angemail" ng-model="angemailModel" ng-pattern="emailAddr" required/> <br /> <br />
<button type="button" ng-click="submitBtn()">
Submit
</button>
</form>
</body>
ng-pattern gives you the option to customize the validation