html代碼javascript
<!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>
js代碼(除了指令意外,沒什麼可用的,寫出來只是爲了,說一下mvc模式而已!)css
var app = angular.module("angularFormCheckModule",[]); /*這裏使用MVC的模式(用來舉例說明MVC而已)*/ app.controller("angularFormCheckCtrl",function($scope,angularFormCheckFactory){//function裏的參數寫你在函數裏須要用到的 $scope.testVar = angularFormCheckFactory.getTest();//這裏就能取到$scope.testVar的值爲---"練習angular表單校驗"; $scope.user = {}; $scope.test= "sss"; }); /*本身能夠去看factory、service、providers的區別(http://www.oschina.net/translate/angularjs-factory-vs-service-vs-provider)*/ /*用 Factory 就是建立一個對象,爲它添加屬性,而後把這個對象返回出來。*/ app.factory('angularFormCheckFactory',function(){ //這裏寫本身的業務邏輯 var test = "練習angular表單校驗"; var service = {};//自定義一個對象 service.getTest = function(){//給對象添加方法 return test; } return service;//返回自定義的service對象!!! }); /*自定義指令--比較兩個密碼是否相等.angular的指令是駝峯的形式(這裏是comparePwd頁面就是compare-pwd)*/ app.directive('comparePwd',function(){ /*angular 自定義指令,可上網自行查找*/ return{ require : 'ngModel', /*scope表示做用域,elem表示使用這個指令的元素對象(這裏指第二個密碼框),attrs。。。ctrl。。。*/ link : function(scope,elem,attrs,ctrl){ /*寫本身的業務邏輯*/ //注意這樣取值的話,第一密碼框的Id值必需要設置且必須與第二個密碼框的compare-pwd屬性的值相同 var firstPwdIdObj = "#" + attrs.comparePwd; $(elem).add(firstPwdIdObj).on('keyup',function(){ /*手動執行髒檢查*/ scope.$apply(function(){ //$(firstPwdIdObj).val()表示第一個密碼框的值。elem.val()表示第二個密碼框的值 var flag = elem.val() === $(firstPwdIdObj).val(); //alert(flag+",--"+elem.val()+",--"+$(firstPwdIdObj).val()); ctrl.$setValidity("pwdmatch",flag);//flag,表示是否相等。pwdmatch用於$error時的標識符,注意看頁面,$setValidity是require中ngModel的方法! }); }); } } });
很差意思今天才知道這個能夠添加代碼,以前寫的博客代碼都好醜,沒有對齊(。。。哈哈,鄉下來的!!!)html