本例通過Angular框架來實現簡單的表單驗證
一、html結構
1、借助于bootstrap快速的編寫了一個簡單的表單
代碼主要部分如下:
<div class="container" ng-controller="myCtrl">
<h2 >用戶表單提交</h2>
<form action="upload.js" class="form-horizontal" name="myForm">
<div class="form-group">
<label for="username" class="col-sm-3 control-label">用戶名</label>
<div class="col-sm-9">
<input type="text" autocomplete="false" name="username" placeholder="用戶名" ng-model="data.username" id="username" class="form-control" required>
<div class="alert alert-danger help-block" >
用戶名長度不能小于5位
</div>
<div class="alert alert-danger help-block" >
用戶名長度不能大于15位
</div>
</div>
</div>
</form>
</div>
2、表單驗證常見問題及指令
1)、問題:
》如何綁定數據——雙向綁定
》驗證表單——正則表達式
》顯示錯誤信息
》整個Form的驗證
》避免提交沒通過驗證的表單
》防止多次提交
2)、常用指令
》指令:
ng-model,ng-required,ng-pattern,ng-maxlength,ng-minlength,ng-change
》樣式:
ng-valid,ng-invalid,ng-pristine,ng-dirty
》form控制變量
formName.inputFieldName.$error:字段錯誤信息
formName.inputFieldName.$dirty:字段是否修改
formName.inputFieldName.$pristine:字段是否是初始狀態
formName.inputFieldName.$valid:字段是否有效
formName.inputFieldName.$invalid:字段是否無效
二、功能實現
2.1 用戶名驗證
用戶輸入用戶名字段驗證主要是長度驗證,以及是否必須
1、ng-model綁定表單數據,以便使用angular的驗證api
2、ng-minlength,ng-maxlength規定字段長段,required規定字段必填
3、錯誤提示信息通過formName.inputFieldName.$error.minlength/maxlength決定是否要顯示,當輸入不合法時,$error對應的錯誤信息會為true
如下:
<div class="form-group">
<label for="username" class="col-sm-3 control-label">用戶名</label>
<div class="col-sm-9">
<input type="text" autocomplete="false" name="username" placeholder="用戶名" ng-model="data.username" id="username" ng-minlength="5" ng-maxlength="15" class="form-control" required>
<div class="alert alert-danger help-block" ng-show="myForm.username.$error.minlength">
用戶名長度不能小于5位
</div>
<div class="alert alert-danger help-block" ng-show="myForm.username.$error.maxlength">
用戶名長度不能大于15位
</div>
</div>
</div>
2.2 密碼確認驗證
用戶密碼確認驗證需要保證兩次輸入的密碼一致,且在未輸入確認密碼前不驗證
1、綁定數據ng-model=data.pwdConfirm(所有表單數據都保存到data對象中)
2、通過判斷data.pwdConfirm!==data.password確定兩次密碼是否輸入一致
3、通過formName.inputField.$dirty確定此項是否填寫過
<div class="form-group">
<label class="col-sm-3 control-label">確認密碼</label>
<div class="col-sm-9">
<input type="password" name="pwdConfirm" ng-model="data.pwdConfirm" placeholder="確認密碼" id="pwdConfirm" required class="form-control">
<div class="alert alert-danger" ng-show="data.pwdConfirm!==data.password&&myForm.pwd.$dirty">
兩次輸入的密碼不一致
</div>
</div>
</div>
2.3 郵箱驗證
郵箱驗證主要驗證郵箱格式是否正確,字段長度
1、使用H5中新增type屬性值email作為
2、通過ng-pattern指令定義郵箱驗證的正則
3、郵箱驗證常用正則表達式:^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+
4、通過myForm.email.$error.pattern驗證郵箱格式是否正確
<div class="form-group">
<label class="col-sm-3 control-label">郵箱</label>
<div class="col-sm-9">
<input type="email" name="email" ng-model="data.email" placeholder="郵箱" class="form-control" required ng-pattern="/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/">
<div class="alert alert-danger help-block" ng-show="myForm.email.$error.minlength">
郵箱長度不能小于5位
</div>
<div class="alert alert-danger help-block" ng-show="myForm.email.$error.pattern">
請填寫正確的郵箱格式
</div>
<div class="alert alert-danger help-block" ng-show="myForm.email.$error.maxlength">
郵箱長度不能大于30位
</div>
</div>
</div>
2.4 單復選框
單復選框主要確定數據綁定問題,以及復選框的數據渲染
1、通過以data對象的屬性形式來綁定數據
2、多選的選項值通過ng-repeat進行遍歷
3、設置value值以便提交時對值進行區分
<div class="form-group">
<label class="col-sm-3 control-label">性別</label>
<div class="col-sm-9">
<label class="radio-inline">
<input type="radio" name="sex" value="1" ng-model="data.sex" />男
</label>
<label class="radio-inline">
<input type="radio" name="sex" value="0" ng-model="data.sex" />女
</label>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">愛好</label>
<div class="col-sm-9">
<label class="radio-inline" ng-repeat="hobby in hobbies">
<input type="checkbox" name="hobby" ng-model="hobbiesC" value="{{hobby.id}}" /> {{hobby.name}}
</label>
</div>
</div>
<div class="col-sm-9 col-sm-offset-3">
<input type="reset" class=" btn btn-danger" value="重置">
<input type="submit" class="btn btn-primary " value="提交">
</div>
2.5 城市二級聯動
1、城市數據定義:每個城市對象包括id,parent,name屬性,id為每個城市省份獨有,parent是根據父級省份或城市而定
2、通過ng-options指令來遍歷出城市數據
2.5.1 城市數據模型
通過\$scope.cities定義數據模型
$scope.cities=[
{
id:1,
parent:0,
name:'四川省'
},
{
id:2,
parent:1,
name:'成都市'
},
...
]
2.5.2 html中渲染城市數據
通過ng-options指令和ng-model指令遍歷數據和設置默認值
<div class="form-group">
<label class="col-sm-3 control-label">出生地</label>
<div class="col-sm-3">
<select name="birthAddress" id="" ng-model="data.province" ng-options="c.id as c.name for c in cities">
<option value="">-- 請選擇 --</option>
</select>
</div>
</div>
說明:
1、ng-options=”obj.name for obj in datas” 常見用法,通過 obj.id as obj.name設置option標簽的value值為id,內容為name
2、ng-model可用于設置select的默認值
2.5.3 定義過濾器用于過濾省份,城市等
.filter("cityFilter",function(){
return function(input,parent){
var filtedData=[];
angular.forEach(input,function(value,key){
if(value.parent===parent){
filtedData.push(value)
}
})
return filtedData;
}
})
說明:
1、如上通過定義cityFilter,傳入一個parent作為參數,遍歷傳入的數據,并寬判斷是否與傳入的parent相等來確定當前城市是哪一級
2、返回的filtedData即是過濾結果。
3、用到了angular.forEach(obj,fn)迭代器,其中fn有三個參數傳入,分別是value,key,obj;value即為當前遍歷出的對象,key為遍歷的唯一標識,obj為被遍歷的數組或對象本身。
使用(省份): ng-options="c.id as c.name for c in cities|cityFilter:0"
2)、城市對應選擇
1、根據data.province作為過濾參數,進行城市篩選
2、區域的選擇同理,可設置當選擇了城市后,再出現區域選項框(ng-show指令)
<div class="col-sm-2">
<select name="birthC" ng-model="data.cities" ng-options="c.id as c.name for c in cities|cityFilter:data.province">
<option value="">-- 請選擇 --</option>
</select>
</div>
<div class="col-sm-2" ng-show="!myForm.birthC.$pristine">
<select name="birthD" ng-model="data.district" ng-options="c.id as c.name for c in cities|cityFilter:data.cities">
<option value="">-- 請選擇 --</option>
</select>
</div>
2.6 表單提交
1、功能需求:當表單驗證不通過時,使提交按鈕失效(ng-disabled),
html結構:
<div class="col-sm-9 col-sm-offset-3">
<input type="reset" class=" btn btn-danger" value="重置" ng-click="reset()">
<input type="submit" class="btn btn-primary " value="提交" ng-disabled="myForm.$invalid">
</div>
js:
$scope.reset=function(){
$scope.myForm.$setPristine();
}
注:表單中有一個$setPristine()方法將表單復位原始狀態,class,$dirty,$pristine會恢復原始狀態。但注意表單的錯誤信息$error并不會被隱藏,所以在驗證表單時,記得在每一項錯誤信息都是由$dirty才判斷的。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。