Angularjs provides form validation functionality. Although it is not as good as the verification methods of other frameworks. But I personally think it is more flexible than other verification frameworks. Not simply by setting properties to achieve.
The ng-show property can control the showing and hiding of a label.
An example with the help of a rookie tutorial is as follows:
We can also specify multiple error messages by [$error.validation name].
Where $error.required is required to enter authentication information. The required attribute when used on the label is HTML5 form validation.
Similarly, the input box of type="email" is also an HTML5 form validation. And ng-maxlength/ng-minlength uses instruction validation.
That is to say, the validation of angularjs is combined with the HTML5 form validation specification.
code show as below;
Then we put the defined directive on the label. Display control of error information through $error.phone.
The ng-show property can control the showing and hiding of a label.
An example with the help of a rookie tutorial is as follows:
<input type="checkbox" ng-model="myVar"> <div ng-show="myVar"> <h1>Welcome</h1> <p>Welcome to my home.</p> </div>
When the checkbox is checked, the div content is displayed. Instead, hide.
Passing validation is controlled by ng-show set on the label.
***.$touch&&****.$invalid is the condition to verify whether it is displayed.
*** is an element. Generally speaking, it is displayed when an element in the form is touched (which can be understood as a focus event) and the validation fails.
<input type="text" name="firstName" ng-model="user.firstName" required /> <span ng-show="myForm.firstName.$touched && myForm.firstName.$invalid" style="color:red;">First Name is required.</span>
We can also specify multiple error messages by [$error.validation name].
Email: <input type="email" name="myEmail" ng-model="myEmail" required /> <span ng-show="myForm.myEmail.$touched && myForm.myEmail.$invalid" style="color:red;"> <span ng-show="myForm.myEmail.$error.required">Email is required.</span> <span ng-show="myForm.myEmail.$error.email">Invalid email format.</span> </span>
Where $error.required is required to enter authentication information. The required attribute when used on the label is HTML5 form validation.
Similarly, the input box of type="email" is also an HTML5 form validation. And ng-maxlength/ng-minlength uses instruction validation.
That is to say, the validation of angularjs is combined with the HTML5 form validation specification.
code show as below;
Email: <input type="email" name="myEmail" ng-model="myEmail" required ng-maxlength="10" /> <span ng-show="myForm.myEmail.$touched && myForm.myEmail.$invalid" style="color:red;"> <span ng-show="myForm.myEmail.$error.required">Email is required.</span> <span ng-show="myForm.myEmail.$error.email">Invalid email format.</span> <span ng-show="myForm.myEmail.$error.maxlength">10max length</span> </span>We can also implement more free verification methods through custom instructions. In the following code we define a validation called phone and add it to the current controller.
//Use directives to customize form validation rules myApp.directive("myValDirective", function() { return { require: "ngModel", link: function(scope, element, attr, mCtrl) { function myValidation(value) { if (/^1[34578]\d{9}$/.test(value)) { mCtrl.$setValidity('phone', true); } else { mCtrl.$setValidity('phone', false); } return value; } mCtrl.$parsers.push(myValidation); } }; });
Then we put the defined directive on the label. Display control of error information through $error.phone.
Phone: <input type="text" name="myPhone" ng-model="myPhone" required my-val-directive /> <span ng-show="myForm.myPhone.$touched && myForm.myPhone.$invalid" style="color:red;"> <span ng-show="myForm.myPhone.$error.required">Phone is required.</span> <span ng-show="myForm.myPhone.$error.phone">Invalid Phone format.</span> </span>The complete code above is as follows:
<!DOCTYPE html> <html ng-app="myApp"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> <title>Form - AngularJS Test</title> <style type="text/css"> .test-div {margin:15px;padding:15px;border:1px solid #ccc;} input.ng-invalid {background-color:#FFFF00;} </style> </head> <body> <div class="test-div" ng-controller="myCtrl"> <form name="myForm" action="#" novalidate> <p> First Name: <input type="text" name="firstName" ng-model="user.firstName" required /> <span ng-show="myForm.firstName.$touched && myForm.firstName.$invalid" style="color:red;">First Name is required.</span> </p> <p> Last Name: <input type="text" name="lastName" ng-model="user.lastName" required /> <span ng-show="myForm.lastName.$touched && myForm.lastName.$invalid" style="color:red;">Last Name is required.</span> </p> <p> Email: <input type="email" name="myEmail" ng-model="myEmail" required ng-maxlength="10" /> <span ng-show="myForm.myEmail.$touched && myForm.myEmail.$invalid" style="color:red;"> <span ng-show="myForm.myEmail.$error.required">Email is required.</span> <span ng-show="myForm.myEmail.$error.email">Invalid email format.</span> <span ng-show="myForm.myEmail.$error.maxlength">10max length</span> </span> </p> <p> Phone: <input type="text" name="myPhone" ng-model="myPhone" required my-val-directive /> <span ng-show="myForm.myPhone.$touched && myForm.myPhone.$invalid" style="color:red;"> <span ng-show="myForm.myPhone.$error.required">Phone is required.</span> <span ng-show="myForm.myPhone.$error.phone">Invalid Phone format.</span> </span> </p> <button ng-click="resetForm()">Reset</button> </form> <p>form = {{user}}</p> <p>origin = {{origin}}</p> </div> <script type="text/javascript" src="static/js/angular-1.5.8.js"></script> <script type="text/javascript"> /** * Forms in AngularJS provides data-binding and validation of input controls: input select button textarea. * AngularJS offers client-side form validation. */ var myApp = angular.module("myApp", []); //Use directives to customize form validation rules myApp.directive("myValDirective", function() { return { require: "ngModel", link: function(scope, element, attr, mCtrl) { function myValidation(value) { if (/^1[34578]\d{9}$/.test(value)) { mCtrl.$setValidity('phone', true); } else { mCtrl.$setValidity('phone', false); } return value; } mCtrl.$parsers.push(myValidation); } }; }); myApp.controller("myCtrl", function($scope) { $scope.origin = {"firstName":"Neo","lastName":"Huang"}; $scope.resetForm = function() { $scope.user = angular.copy($scope.origin); }; $scope.resetForm(); }); </script> </body> </html>