亲宝软件园·资讯

展开

AngularJs表单校验功能实例代码

寂寞的天空GM 人气:0

废话不多说了,具体代码如下所示:

<!DOCTYPE html>
<html ng-app="angularFormCheckModule">
 <head>
  <meta charset="UTF-8">
  <title>angular表单校验</title>
  <link rel="stylesheet" href="../css/bootstrap.min.css" rel="external nofollow" />
  <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模式而已!)

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的方法!
    });
   });
  }
 }
});

下面看一段代码关于AngularJs获取焦点与失去焦点时的表单验证

<!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">必填项</span><br> 
</form> 
</body> 
</html> 

以上所述是小编给大家介绍的AngularJs表单校验功能实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

加载全部内容

相关教程
猜你喜欢
用户评论