AngularJS实现表单手动验证和表单自动验证
AngularJS的表单验证大致有两种,一种是手动验证,一种是自动验证。
一、手动验证
所谓手动验证是通过AngularJS表单的属性来验证。而成为AngularJS表单必须满足两个条件:
1、给form元素加上novalidate="novalidate";
2、给form元素加上name="theForm",如下:
<!DOCTYPE html>
<
html
lang
=
"en"
ng-app
=
"myApp1"
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
title
></
title
>
<
link
rel
=
"stylesheet"
href
=
"../node_modules/bootstrap/dist/css/bootstrap.min.css"
/>
<
link
rel
=
"stylesheet"
href
=
"../css/main.css"
/>
</
head
>
<
body
>
<
nav
>
<
div
class
=
"container"
>
<
div
class
=
"navbar-header"
>
<
a
href
=
"/"
class
=
"navbar-brand"
>Form Submitting</
a
>
</
div
>
</
div
>
</
nav
>
<
div
class
=
"container main-content"
ng-controller
=
"myCtrl1"
>
<!--novalidate让表单不要使用html验证-->
<!--theForm变成scope的一个字段-->
<
form
ng-submit
=
"onSubmit(theForm.$valid)"
novalidate
=
"novalidate"
name
=
"theForm"
>
<
div
class
=
"form-group"
>
<
label
for
=
"name"
>Name</
label
>
<
input
type
=
"text"
class
=
"form-control"
id
=
"name"
ng-model
=
"formModel.name"
/>
</
div
>
<
div
class
=
"form-group"
ng-class="{
'has-error': !theForm.email.$valid && (!theForm.$pristine || theForm.$submitted),
'has-success': theForm.email.$valid && (!theForm.$pristine || theForm.$submitted)
}">
<
label
for
=
"email"
>Email</
label
>
<
input
type
=
"email"
class
=
"form-control"
id
=
"email"
ng-model
=
"formModel.email"
required
=
"required"
name
=
"email"
/>
<
p
class
=
"help-block"
ng-show
=
"theForm.email.$error.required && (!theForm.$pristine || theForm.$submitted)"
>必填</
p
>
<
p
class
=
"help-block"
ng-show
=
"theForm.email.$error.email && (!theForm.$pristine || theForm.$submitted)"
>email格式不正确</
p
>
</
div
>
<
div
class
=
"form-group"
>
<
label
for
=
"username"
>Username</
label
>
<
input
type
=
"text"
class
=
"form-control"
id
=
"username"
ng-model
=
"formModel.username"
/>
</
div
>
<
div
class
=
"form-group"
>
<
label
for
=
"age"
>Age</
label
>
<
input
type
=
"number"
class
=
"form-control"
id
=
"age"
ng-model
=
"formModel.age"
/>
</
div
>
<
div
class
=
"form-group"
>
<
label
for
=
"sex"
>Sex</
label
>
<
select
name
=
"sex"
id
=
"sex"
class
=
"form-control"
ng-model
=
"formModel.sex"
>
<
option
value
=
""
>Please choose</
option
>
<
option
value
=
"male"
>Mail</
option
>
<
option
value
=
"femail"
>Femail</
option
>
</
select
>
</
div
>
<
div
class
=
"form-group"
>
<
label
for
=
"password"
>Password</
label
>
<
input
type
=
"text"
class
=
"form-control"
id
=
"password"
ng-model
=
"formModel.password"
/>
</
div
>
<
div
class
=
"form-group"
>
<
button
class
=
"btn btn-primary"
type
=
"submit"
>Register</
button
>
</
div
>
<
pre
>
{{theForm | json}}
</
pre
>
</
form
>
</
div
>
<
script
src
=
"../node_modules/angular/angular.min.js"
></
script
>
<
script
src
=
"second.js"
></
script
>
</
body
>
</
html
>
● 给form加上novalidate="novalidate"意味着表单将不再使用HTML5验证特性
● 给form加上name="theForm"意味着表单的名称是theForm。如何使用theForm,比如我们验证表单是否被修改过theForm.$submitted
● 通过ng-submit提交表单
● formModel是$scope中的一个属性
● 对表单的Email进行了手动验证,使用了AngularJS表单的众多属性,比如theForm.email.$valid,theForm.$pristine,theForm.$submitted, theForm.email.$error.required,theForm.email.$error.email
● 通过<pre>{{theForm | json}}</pre>把AngularJS表单的所有属性都打印出来
{
"$error": {
"required": [
{
"$validators": {},
"$asyncValidators": {},
"$parsers": [],
"$formatters": [
null
],
"$viewChangeListeners": [],
"$untouched": true,
"$touched": false,
"$pristine": true,
"$dirty": false,
"$valid": false,
"$invalid": true,
"$error": {
"required": true
},
"$name": "email",
"$options": null
}
]
},
"$name": "theForm",
"$dirty": false,
"$pristine": true,
"$valid": false,
"$invalid": true,
"$submitted": false,
"email": {
"$validators": {},
"$asyncValidators": {},
"$parsers": [],
"$formatters": [
null
],
"$viewChangeListeners": [],
"$untouched": true,
"$touched": false,
"$pristine": true,
"$dirty": false,
"$valid": false,
"$invalid": true,
"$error": {
"required": true
},
"$name": "email",
"$options": null
},
"sex": {
"$validators": {},
"$asyncValidators": {},
"$parsers": [],
"$formatters": [],
"$viewChangeListeners": [],
"$untouched": true,
"$touched": false,
"$pristine": true,
"$dirty": false,
"$valid": true,
"$invalid": false,
"$error": {},
"$name": "sex",
"$options": null
}
}
在second.js文件中定义了module,controller以及提交表单的方法。
var
myApp1 = angular.module(
'myApp1'
,[]);
myApp1.controller(
'myCtrl1'
,
function
($scope, $http){
$scope.formModel = {};
$scope.onSubmit =
function
(){
$http.post(
'someurl'
,$scope.formModel)
.success(
function
(data){
console.log(
':)'
);
})
.error(
function
(data){
console.log(
':('
);
});
console.log($scope.formModel);
};
});
二、自动验证
AngularJS的另外一种表单验证方式是自动验证,即通过directive来实现,除了AngularJS自带的directive,还需要用到angular-auto-validate这个第三方module。
有关angular-auto-validate:
● 安装:npm i angular-auto-validate
● 引用:<script src="../node_modules/angular-auto-validate/dist/jcs-auto-validate.min.js"></script>
● module依赖:var myApp = angular.module("app", ["jcs-autoValidate"]);
为了实现错误信息本地化,还需要angular-localize这个第三方module:
● 安装:npm install angular-localize --save
● module依赖:var myApp = angular.module("app", ["localize"]);
<script src=
"../node_modules/angular-sanitize/angular-sanitize.min.js"
></script>
<script src=
"../node_modules/angular-localize/angular-localize.min.js"
></script>
此外,当点击提交表单按钮,需要禁用按钮并显示一种等待效果,需要用到angular-ladda这个第三方module:
● 安装:bower install angular-ladda --save
● module依赖:var myApp = angular.module("app", ["angular-ladda"]);
● 引用:
<link rel=
"stylesheet"
href=
"../bower_components/ladda/dist/ladda-themeless.min.css"
/>
<script src=
"../bower_components/ladda/dist/spin.min.js"
></script>
<script src=
"../bower_components/ladda/dist/ladda.min.js"
></script>
<script src=
"../bower_components/angular-ladda/dist/angular-ladda.min.js"
></script>
<!DOCTYPE html>
<
html
lang
=
"en"
ng-app
=
"myApp1"
>
<
head
>
<
meta
charset
=
"gb2312"
>
<
title
></
title
>
<
link
rel
=
"stylesheet"
href
=
"../node_modules/bootstrap/dist/css/bootstrap.min.css"
/>
<
link
rel
=
"stylesheet"
href
=
"../bower_components/ladda/dist/ladda-themeless.min.css"
/>
<
link
rel
=
"stylesheet"
href
=
"../css/main.css"
/>
</
head
>
<
body
>
<
nav
>
<
div
class
=
"container"
>
<
div
class
=
"navbar-header"
>
<
a
href
=
"/"
class
=
"navbar-brand"
>Form Validating Auto</
a
>
</
div
>
</
div
>
</
nav
>
<
div
class
=
"container main-content"
ng-controller
=
"myCtrl1"
>
<!--novalidate让表单不要使用html验证-->
<!--theForm变成scope的一个字段-->
<
form
ng-submit
=
"onSubmit()"
novalidate
=
"novalidate"
>
<
div
class
=
"form-group"
>
<
label
for
=
"name"
class
=
"control-label"
>Name</
label
>
<
input
type
=
"text"
class
=
"form-control"
id
=
"name"
ng-model
=
"formModel.name"
required
=
"required"
/>
</
div
>
<
div
class
=
"form-group"
>
<
label
for
=
"email"
class
=
"control-label"
>Email</
label
>
<
input
type
=
"email"
class
=
"form-control"
id
=
"email"
ng-model
=
"formModel.email"
required
=
"required"
/>
</
div
>
<
div
class
=
"form-group"
>
<
label
for
=
"username"
class
=
"control-label"
>Username</
label
>
<
input
type
=
"text"
class
=
"form-control"
id
=
"username"
ng-model
=
"formModel.username"
required
=
"required"
ng-pattern
=
"/^[A-Za-z0-9_]{1,32}$/"
ng-minlength
=
"7"
ng-pattern-err-type
=
"badUsername"
/>
</
div
>
<
div
class
=
"form-group"
>
<
label
for
=
"age"
class
=
"control-label"
>Age</
label
>
<
input
type
=
"number"
class
=
"form-control"
id
=
"age"
ng-model
=
"formModel.age"
required
=
"required"
min
=
"18"
max
=
"65"
ng-min-err-type
=
"tooYoung"
ng-max-err-type
=
"tooOld"
/>
</
div
>
<
div
class
=
"form-group"
>
<
label
for
=
"sex"
class
=
"control-label"
>Sex</
label
>
<
select
name
=
"sex"
id
=
"sex"
class
=
"form-control"
ng-model
=
"formModel.sex"
required
=
"required"
>
<
option
value
=
""
>Please choose</
option
>
<
option
value
=
"male"
>Mail</
option
>
<
option
value
=
"femail"
>Femail</
option
>
</
select
>
</
div
>
<
div
class
=
"form-group"
>
<
label
for
=
"password"
class
=
"control-label"
>Password</
label
>
<
input
type
=
"text"
class
=
"form-control"
id
=
"password"
ng-model
=
"formModel.password"
required
=
"required"
ng-minlength
=
"6"
/>
</
div
>
<
div
class
=
"form-group"
>
<!--<button class="btn btn-primary" ng-click="onSubmit()">Register</button>-->
<
button
class
=
"btn btn-primary"
ladda
=
"submitting"
data-style
=
"expand-right"
type
=
"submit"
>
<
span
ng-show
=
"submitting"
>正在注册...</
span
>
<
span
ng-show
=
"!submitting"
>注册</
span
>
</
button
>
</
div
>
<
pre
>
{{formModel | json}}
</
pre
>
</
form
>
</
div
>
<
script
src
=
"../node_modules/angular/angular.min.js"
></
script
>
<
script
src
=
"form_validation_auto.js"
></
script
>
<
script
src
=
"../node_modules/angular-auto-validate/dist/jcs-auto-validate.min.js"
></
script
>
<
script
src
=
"../node_modules/angular-sanitize/angular-sanitize.min.js"
></
script
>
<
script
src
=
"../node_modules/angular-localize/angular-localize.min.js"
></
script
>
<
script
src
=
"../bower_components/ladda/dist/spin.min.js"
></
script
>
<
script
src
=
"../bower_components/ladda/dist/ladda.min.js"
></
script
>
<
script
src
=
"../bower_components/angular-ladda/dist/angular-ladda.min.js"
></
script
>
以上,先看提交按钮:
<
div
>
<!--<button class="btn btn-primary" ng-click="onSubmit()">Register</button>-->
<
button
class
=
"btn btn-primary"
ladda
=
"submitting"
data-style
=
"expand-right"
type
=
"submit"
>
<
span
ng-show
=
"submitting"
>正在注册...</
span
>
<
span
ng-show
=
"!submitting"
>注册</
span
>
</
button
>
</
div
>
● ladda属性值为bool值,true表示显示动态等待效果,false不显示动态等待效果,这里的submitting是scope中的一个属性
● data-style="expand-right"表示在按钮的右侧显示动态等待效果
再拿表单中的Age字段来说:
<
div
>
<
label
for
=
"age"
class
=
"control-label"
>Age</
label
>
<
input
type
=
"number"
class
=
"form-control"
id
=
"age"
ng-model
=
"formModel.age"
required
=
"required"
min
=
"18"
max
=
"65"
ng-min-err-type
=
"tooYoung"
ng-max-err-type
=
"tooOld"
/>
</
div
>
是在module层面用上了,定义在了form_validation_auto.js文件中。
var
myApp1 = angular.module(
'myApp1'
,[
'jcs-autoValidate'
,
'localize'
,
'angular-ladda'
]);
myApp1.run(
function
(defaultErrorMessageResolver){
defaultErrorMessageResolver.getErrorMessages().then(
function
(errorMessages){
errorMessages[
'tooYoung'
] =
'年龄必须小于{0}'
;
errorMessages[
'tooOld'
] =
'年龄不能大于{0}'
;
errorMessages[
'badUsername'
] =
'用户名只能包含数字、字母或下划线'
;
});
});
myApp1.controller(
'myCtrl1'
,
function
($scope, $http){
$scope.formModel = {};
$scope.submitting =
false
;
$scope.onSubmit =
function
(){
$scope.submitting =
true
;
console.log(
'已提交'
);
console.log($scope.formModel);
$http.post(
'url'
,$scope.formModel)
.success(
function
(data){
console.log(
':)'
);
$scope.submitting =
false
;
})
.error(
function
(data){
console.log(
':('
);
$scope.submitting =
false
;
});
};
});
AngularJS实现表单手动验证和表单自动验证的更多相关文章
- Thinkphp框架 表单自动验证登录注册 ajax自动验证登录注册
说明:这里没练习静态自动验证:如果用到静态验证首先自定义一个控制器,再在Model文件夹里创建一个NiHaoModel.php 类 NiHao是自定义的,前缀可以随意,但是一定要用驼峰法(首字母大写 ...
- tp框架之自动验证表单
tp框架的create自动加载表单的方法可以自动根据自己定义的要求来验证表单里面的内容,但是由于是在后台执行代码,会拖慢程序运行速度,所以还是建议通过前端js来进行判断,后台只进行数据库的查询以及传值 ...
- ThinkPHP3.1.3分表状态时候的自动验证的代码BUG
问题描述 ThinkPHP3.1.3 当使用TP的分库分表后 有些地方需要使用Model自动验证create,当验证唯一性unique会出现BUG, 具体描述 因为自动验证检测唯一性会使用隐式的使用f ...
- thinkphp自动验证中的静态验证和动态验证和批量验证
1.静态定义 在模型类里面预先定义好该模型的自动验证规则,我们称为静态定义. 举例说明,我们在模型类里面定义了$_validate属性如下: class UserModel extends Model ...
- ThinkPHP3.1快速入门(12)自动验证
自动验证是ThinkPHP模型层提供的一种数据验证方法,可以在使用create创建数据对象的时候自动进行数据验证. 验证规则 数据验证可以进行数据类型.业务规则.安全判断等方面的验证操作.数据验证有两 ...
- ThinkPHP自动验证
自动验证是ThinkPHP模型层提供的一种数据验证方法,可以在使用create创建数据对象的时候自动进行数据验证. 验证规则 数据验证可以进行数据类型.业务规则.安全判断等方面的验证操作.数据验证有两 ...
- ThinkPHP第二十四天(JQuery常用方法、TP自动验证)
---恢复内容开始--- 1.JQuery常用方法 A:JS中可以用json格式数据当做数组使用,如var validate={username:false,pwd:false,pwded:false ...
- Thinkphp 中的自动验证 上一篇有例子
说明,只要定义了Model,在任何地方调用,都会进行验证.很方便. 必须是D方法,才会有效.M方法不会触发自动验证. 说明:这里没练习静态自动验证:如果用到静态验证首先自定义一个控制器,再在Model ...
- tinkphp中的自动验证
tinkphp是国内非常流行的一个开源框架,国内大小公司都在用的框架.对于初学的好多同学感觉不太好上手,其实并没没有大家想的那么复杂.自动验证功能是thinkphp提高的一种数据验证方法,分为动态和静 ...
- thinkphp 自动验证
自动验证是ThinkPHP模型层提供的一种数据验证方法,可以在使用create创建数据对象的时候自动进行数据验证. 大理石平台价格表 验证规则 数据验证可以进行数据类型.业务规则.安全判断等方面的验证 ...
随机推荐
- ubuntu12.04下同步cm10源码(个人记录,当作笔记)
环境:AMD N850,4G,ubuntu12.04 x64, 老规矩,先是各种依赖: sudo apt-get install git-core gnupg flex bison python ra ...
- 剑指offer中二进制中1的个数
容易想到的是将n一位一位的和1进行比较,产生如下代码 但是这样的话会出下面的问题 那么就是原数据不动,将1依次移动进行比较有如下的代码 一种更简单的方法是: 这样做的思路是 总而言之
- MVC几个系统常用的Filter过滤器
1.AcceptVerbs 规定页面的访问形式,如 [AcceptVerbs(HttpVerbs.Post)] public ActionResult Example(){ return View() ...
- 移动互联与O2O的完美衔接
移动互联网虽然市场颇大,前景广阔,但是由于数据过于密集,很难精准的定位所谓的目标客户群,然而O2O的线下市场却与互联网市场有极大的反差.一直觉得高校周边的小商家是最幸福的生意人,客户明确(就是本校学生 ...
- 前端HTML与CSS编码规范
HTML 语法 HTML5 doctype 语言属性(Language attribute) 字符编码 IE 兼容模式 引入 CSS 和 JavaScript 文件 实用为王 属性顺序 布尔(bool ...
- [转] Trie树详解及其应用
一.知识简介 最近在看字符串算法了,其中字典树.AC自动机和后缀树的应用是最广泛的了,下面将会重点介绍下这几个算法的应用. 字典树(Trie)可以保存一些字符串->值 ...
- Open Session In View
Open Session In View模式的主要思想是:在用户的每一次请求过程始终保持一个Session对象打开着 实现步骤: 步骤一.创建一个Web项目,创建包cn.happy.util,创建Hi ...
- Multithreading: How to Use the Synchronization Classes
(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu 转载请标明来源) 翻译文章来源: msdn - Multithreading: How to Use t ...
- swift调用相机和相册
简单实现swift调用相机和相册的功能,分享代码与学习swift的童鞋共同进步 import UIKit class ViewController: UIViewController,UIImageP ...
- Unity 3D 动画帧事件
前几天在项目开发中碰到一个这样的需求,RPG游戏中,特效和动画播放不同步的.假如主角在攻击NPC时,先实例化特效,后播放动画.动画毕竟是有一个时间长度的.等到动画播放攻击挥刀的那一瞬间时,特效可能早就 ...