欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AngularJs表單校驗(yàn)功能實(shí)例代碼

 更新時(shí)間:2017年02月09日 13:55:05   作者:寂寞的天空GM  
這篇文章主要介紹了AngularJs表單校驗(yàn)功能實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下

廢話不多說了,具體代碼如下所示:

<!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,不然無法執(zhí)行在臟檢查,就無法校驗(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" style="color: green;">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代碼(除了指令意外,沒什么可用的,寫出來只是為了,說一下mvc模式而已?。?/p>

var app = angular.module("angularFormCheckModule",[]);
/*這里使用MVC的模式(用來舉例說明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ì)象返回出來。*/
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頁面就是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í)符,注意看頁面,$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ì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論