angularjs速成学习个人理解_9表单验证

来源:互联网 发布:看太空的软件 编辑:程序博客网 时间:2024/05/22 13:07
angularjs提供表单验证功能。虽然比不上其他框架的的验证方式的。但是个人认为比别的验证框架要灵活一些。不是简单的通过设置属性来实现。
ng-show属性可以控制一个标签的显示与隐藏。
借助菜鸟教程的例子如下:
 <input type="checkbox" ng-model="myVar"><div ng-show="myVar"><h1>Welcome</h1><p>Welcome to my home.</p></div>


当checkbox选中时,div内容显示。反之,隐藏。
通过设置在标签上的ng-show控制验证是否通过。
***.$touch&&****.$invalid就是验证是否显示的条件。
***是某个元素。大体意思是,表单中某个元素被触碰(可以理解为焦点事件)且验证失败时显示。

<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>



我们也可以通过【$error.验证名】方式指定多个错误信息。
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>


其中$error.required为必须输入验证信息。而在标签上用到时的required属性,这个属性是HTML5的表单验证。
同理,type="email"这个输入框也是HTML5的表单验证。而ng-maxlength/ng-minlength则利用的是指令验证。
也就是说angularjs的验证是结合了HTML5表单验证规范。
代码如下;

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>
我们也可以通过自定义指令来实现更加自由的验证方式。如下代码我们定义了一个叫做phone的验证,并加入到当前控制器中。

//使用指令自定义表单验证规则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);    }    };});

然后我们把定义好的指令放在标签上。通过$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>
以上完整代码如下:

<!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", []);//使用指令自定义表单验证规则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>