angularJs 表單校驗 (手機號,使用者名稱,密碼等)
<!DOCTYPE html>
<html ng-app="angularFormCheckModule">
<head>
<meta charset="UTF-8">
<title>angular表單校驗</title>
<link rel="stylesheet" href="../css/bootstrap.min.css" />
<style>
span{
color: red;
}
</style>
</head>
<body ng-controller="angularFormCheckCtrl">
<!--使用angular校驗,每一個校驗的項都必須用ng-model,不然無法執行在髒檢查,就無法校驗-->
<form name="angularForm" novalidate method="post">
<table class="table table-bordered">
<tr>
<td>使用者名稱</td>
<td>
<input type="number" required="required" ng-model="user.userName" name="userName" ng-minlength="6"/>
<!--angularForm.userName.$dirty檢查是否是第一次輸入!網上有很多種方法校驗是否是第一次輸入-->
<span class="warning" ng-show="angularForm.userName.$dirty && angularForm.userName.$error.required">*</span>
<span class="warning" ng-show="angularForm.userName.$error.number">只能輸入數字</span>
<span class="warning" ng-show="angularForm.userName.$error.minlength">最少為6位數</span>
</td>
</tr>
<tr>
<td>密碼</td>
<td>
<!--這裡的id,一定要等於compare-pwd的值,因為指令裡面是根據Id取值的-->
<input type="password" required="required" ng-minlength="6" name="pwd" ng-model="user.password" id="pwd"/>
<!--angularForm.pwd.$pristine首次輸入,不太清楚的就自己執行,去掉條件一個一個的試!-->
<span class="warning" ng-show="!angularForm.pwd.$pristine && angularForm.pwd.$error.required">*</span>
<span class="warning" ng-show="angularForm.pwd.$error.minlength">最少為6位數</span>
</td>
</tr>
<tr>
<td>確認密碼</td>
<td>
<!--這裡compare-pwd的值,要等於被比較的物件的name屬性值,即第一個密碼框的name值-->
<input type="password" required="required" name="pwd2" compare-pwd="pwd" ng-model="pwd2"/>
<span class="warning" ng-show="angularForm.pwd2.$error.required">*</span>
<!--注意這裡的pwdmatch,是指令裡面設定的-->
<span class="warning" ng-show="angularForm.pwd2.$error.pwdmatch">X</span>
<span class="warning" ng-show="angularForm.pwd2.$valid" style="color: green;">OK</span>
<!--
其實這種事最簡單的校驗方式,不用寫指令!!!
<span ng-show="user.password !=pwd2">兩次密碼輸入不一致</span>
-->
</td>
</tr>
<tr>
<td>手機</td>
<td>
<!--pattern正則表示式校驗輸入內容-->
<input type="number" required="required" name="phone" ng-model="user.phone" ng-pattern="/^1[3|4|5|7|8]\d{9}$/">
<span class="warning" ng-show="angularForm.phone.$error.required">*</span>
<span class="warning" ng-show="angularForm.phone.$error.number">只能輸入數字</span>
<span class="warning" ng-show="angularForm.phone.$error.pattern">手機格式不正確</span>
</td>
</tr>
<tr>
<td>郵箱</td>
<td>
<input type="email" required="required" ng-model="user.email" name="email"/>
<span class="warning" ng-show="angularForm.email.$error.required">*</span>
<span class="warning" ng-show="angularForm.email.$error.email">郵箱格式不正確</span>
</td>
</tr>
<tr>
<td>URL</td>
<td>
<input type="url" required="required" ng-model="user.url" name="url"/>
<span class="warning" ng-show="angularForm.url.$error.required">*</span>
<span class="warning" ng-show="angularForm.url.$error.url">URL格式不正確</span>
</td>
</tr>
<tr>
<td>(注:*為必填)</td>
<td>
<input type="submit" value="提交" ng-disabled="!angularForm.$valid" class="btn btn-success"/>
</td>
</tr>
</table>
</form>
</body>
<script type="text/javascript" src="../js/jquery.min.js" ></script>
<script type="text/javascript" src="../js/angular-1.2.22.js" ></script>
<script type="text/javascript" src="../js/angularFormCheck.js" ></script>
</html>