AngularJS的表單驗證大體有兩種,一種是手動驗證,一種是自動驗證。
1、手動驗證
所謂手動驗證是經過AngularJS表單的屬性來驗證。而成爲AngularJS表單必須知足兩個條件:css
一、給form元素加上novalidate="novalidate";html
二、給form元素加上name="theForm",以下:node
{
"$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
}
}
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);
};
});
<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:npm
● 安裝:bower install angular-ladda --save
● module依賴:var myApp = angular.module("app", ["angular-ladda"]);
● 引用:json
<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
>
以上,先看提交按鈕:bootstrap