廢話不多說(shuō)了,具體代碼如下所示:
成都創(chuàng)新互聯(lián)公司長(zhǎng)期為上1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為貴州企業(yè)提供專業(yè)的網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站,貴州網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
<!DOCTYPE html> <html ng-app="angularFormCheckModule"> <head> <meta charset="UTF-8"> <title>angular表單校驗(yàn)</title> <link rel="stylesheet" href="../css/bootstrap.min.css" rel="external nofollow" /> <style> span{ color: red; } </style> </head> <body ng-controller="angularFormCheckCtrl"> <!--使用angular校驗(yàn),每一個(gè)校驗(yàn)的項(xiàng)都必須用ng-model,不然無(wú)法執(zhí)行在臟檢查,就無(wú)法校驗(yàn)--> <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檢查是否是第一次輸入!網(wǎng)上有很多種方法校驗(yàn)是否是第一次輸入--> <span class="warning" ng-show="angularForm.userName.$dirty && angularForm.userName.$error.required">*</span> <span class="warning" ng-show="angularForm.userName.$error.number">只能輸入數(shù)字</span> <span class="warning" ng-show="angularForm.userName.$error.minlength">最少為6位數(shù)</span> </td> </tr> <tr> <td>密碼</td> <td> <!--這里的id,一定要等于compare-pwd的值,因?yàn)橹噶罾锩媸歉鶕?jù)Id取值的--> <input type="password" required="required" ng-minlength="6" name="pwd" ng-model="user.password" id="pwd"/> <!--angularForm.pwd.$pristine首次輸入,不太清楚的就自己運(yùn)行,去掉條件一個(gè)一個(gè)的試!--> <span class="warning" ng-show="!angularForm.pwd.$pristine && angularForm.pwd.$error.required">*</span> <span class="warning" ng-show="angularForm.pwd.$error.minlength">最少為6位數(shù)</span> </td> </tr> <tr> <td>確認(rèn)密碼</td> <td> <!--這里compare-pwd的值,要等于被比較的對(duì)象的name屬性值,即第一個(gè)密碼框的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,是指令里面設(shè)置的--> <span class="warning" ng-show="angularForm.pwd2.$error.pwdmatch">X</span> <span class="warning" ng-show="angularForm.pwd2.$valid" >OK</span> <!-- 其實(shí)這種事最簡(jiǎn)單的校驗(yàn)方式,不用寫指令!!! <span ng-show="user.password !=pwd2">兩次密碼輸入不一致</span> --> </td> </tr> <tr> <td>手機(jī)</td> <td> <!--pattern正則表達(dá)式校驗(yàn)輸入內(nèi)容--> <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">只能輸入數(shù)字</span> <span class="warning" ng-show="angularForm.phone.$error.pattern">手機(jī)格式不正確</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代碼(除了指令意外,沒(méi)什么可用的,寫出來(lái)只是為了,說(shuō)一下mvc模式而已!)
var app = angular.module("angularFormCheckModule",[]); /*這里使用MVC的模式(用來(lái)舉例說(shuō)明MVC而已)*/ app.controller("angularFormCheckCtrl",function($scope,angularFormCheckFactory){//function里的參數(shù)寫你在函數(shù)里需要用到的 $scope.testVar = angularFormCheckFactory.getTest();//這里就能取到$scope.testVar的值為---"練習(xí)angular表單校驗(yàn)"; $scope.user = {}; $scope.test= "sss"; }); /*自己可以去看factory、service、providers的區(qū)別(http://www.oschina.net/translate/angularjs-factory-vs-service-vs-provider)*/ /*用 Factory 就是創(chuàng)建一個(gè)對(duì)象,為它添加屬性,然后把這個(gè)對(duì)象返回出來(lái)。*/ app.factory('angularFormCheckFactory',function(){ //這里寫自己的業(yè)務(wù)邏輯 var test = "練習(xí)angular表單校驗(yàn)"; var service = {};//自定義一個(gè)對(duì)象 service.getTest = function(){//給對(duì)象添加方法 return test; } return service;//返回自定義的service對(duì)象!!! }); /*自定義指令--比較兩個(gè)密碼是否相等.angular的指令是駝峰的形式(這里是comparePwd頁(yè)面就是compare-pwd)*/ app.directive('comparePwd',function(){ /*angular 自定義指令,可上網(wǎng)自行查找*/ return{ require : 'ngModel', /*scope表示作用域,elem表示使用這個(gè)指令的元素對(duì)象(這里指第二個(gè)密碼框),attrs。。。ctrl。。。*/ link : function(scope,elem,attrs,ctrl){ /*寫自己的業(yè)務(wù)邏輯*/ //注意這樣取值的話,第一密碼框的Id值必須要設(shè)置且必須與第二個(gè)密碼框的compare-pwd屬性的值相同 var firstPwdIdObj = "#" + attrs.comparePwd; $(elem).add(firstPwdIdObj).on('keyup',function(){ /*手動(dòng)執(zhí)行臟檢查*/ scope.$apply(function(){ //$(firstPwdIdObj).val()表示第一個(gè)密碼框的值。elem.val()表示第二個(gè)密碼框的值 var flag = elem.val() === $(firstPwdIdObj).val(); //alert(flag+",--"+elem.val()+",--"+$(firstPwdIdObj).val()); ctrl.$setValidity("pwdmatch",flag);//flag,表示是否相等。pwdmatch用于$error時(shí)的標(biāo)識(shí)符,注意看頁(yè)面,$setValidity是require中ngModel的方法! }); }); } } });
下面看一段代碼關(guān)于AngularJs獲取焦點(diǎn)與失去焦點(diǎn)時(shí)的表單驗(yàn)證
<!DOCTYPE html> <html ng-app="formExample"> <head> <meta charset="UTF-8"> <title></title> <script src="../js/angular.js"></script> <script> angular.module('formExample', []) .controller('FormController', ['$scope', function($scope) { $scope.userType = 'guest'; $scope.change = false; }]); </script> </head> <body> <form name="myForm" ng-controller="FormController"> userType: <input name="input" ng-model="userType" ng-blur="change=true" ng-focus="change=false" required> <span class="error" ng-show="myForm.input.$error.required && change">必填項(xiàng)</span><br> </form> </body> </html>
以上所述是小編給大家介紹的AngularJs表單校驗(yàn)功能實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!
文章題目:AngularJs表單校驗(yàn)功能實(shí)例代碼
轉(zhuǎn)載源于:http://www.chinadenli.net/article38/piopsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、響應(yīng)式網(wǎng)站、網(wǎng)站排名、自適應(yīng)網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)