欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

AngularJs表单校验功能实例代码

程序员文章站 2022-03-07 11:01:24
废话不多说了,具体代码如下所示:

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

<!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表单校验功能实例代码,希望对大家有所帮助