angularjs速成学习个人理解_9表单验证
2017-10-30 10:51
507 查看
angularjs提供表单验证功能。虽然比不上其他框架的的验证方式的。但是个人认为比别的验证框架要灵活一些。不是简单的通过设置属性来实现。
ng-show属性可以控制一个标签的显示与隐藏。
借助菜鸟教程的例子如下:
当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.验证名】方式指定多个错误信息。
其中$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 myVal
4000
idation(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进行错误信息的显示控制。
<!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>
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 myVal
4000
idation(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>
相关文章推荐
- angularjs速成学习个人理解_4依赖注入
- angularjs速成学习个人理解_3表达式filter过滤器的使用
- angularjs速成学习个人理解_7指令Directives
- angularjs速成学习个人理解_6$q服务的promise
- angularjs速成学习个人理解_5$http服务
- angularjs速成学习个人理解_8form中的下拉框
- AngularJS学习记录-表单验证
- JavaScript个人学习记录总结(二)——验证表单输入之模式匹配
- angularjs学习第五天笔记(第二篇:表单验证升级篇)
- angularjs学习第四天笔记(第一篇:简单的表单验证)
- 【js类库AngularJs】学习angularJs的指令(包括常见表单验证,隐藏等功能)
- AngularJS学习之 登录表单 清爽验证(边学边更新)
- angularjs表单验证-学习
- 【AngularJS学习笔记】AngularJS表单验证
- AngularJS学习笔记之表单验证功能实例详解
- angularjs学习 ng-Messages表单验证
- 学习angularjs客户端表单验证
- [Java学习笔记] 对象的比较(引用比较、值比较) ^_^个人的理解,请多多关照^_^
- AngularJS 入门4-表单和验证