我由于制作登陆界面,用到了angular-validation,结合ng-cookies,实现记住账户密码的功能。文档是https://github.com/hueitan/angular-validation/blob/master/API.md

该文档挺详细的介绍了angular-validation的使用方式。我因为英文不好,理解的不是很仔细。但是总结了以下的使用经验。

1 首先在模块中注入validation模块

  var app = angular.module("zdwq",["ui.router","ui.bootstrap","ngCookies","validation"]);

2 config.js文件中配置config ,设置验证规则的表达式以及文字

  app.config(['"$validationProvider",function($validationProvider){

    var expression = {
      username:/^1[3|4|5|7|8]\d{9}$/,
      password: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,18}$/,
      required:function(value){
         return !!value       
      }
      };
    var defaultMsg = {
      username:{
        success:"",
        error:"必须是手机号"
      },
      password:{
        success:"",
        error:"长度不能小于8位,不能大于18位"
      }
    };

    $validationProvider.setExpression(expression).setDefaultMsg(defaultMsg);

3  html结构中使用验证

  <form name="loginForm">
    <p class="pr"><input name="username_account" validator="username" type="text" class="login-name" placeholder="请输入账号" ng-model="user.username" valid-method="blur,submit"></p>
    <p class="pr"><input name="password_account" validator="password" type="password" class="login-pwd" placeholder="请输入密码" ng-model="user.password" valid-method="blur,submit"></p>
    <p><label class="remember-label"><i class="db vm login-checkbox" ng-class="user.remembered?'checked':''" alt=""></i><input type="checkbox" hidden class="login-remember" ng-model="user.remembered">记住账号</label></p>
    <p><button class="login-btn" ng-click="submit(loginForm)" validator-submit="loginForm">登录</button></p>
  </form>

  注意 (1) form指定名字,方便ng-click调用函数作为参数使用$validation.validate(form).success(function(){}).error(function(){});

      $scope.submit = function(form){
        $validation.validate(form).success(function(){
          $http.get("data/login.json").then(function(){
            if($scope.user.remembered){
              $cookies.put("zdwq_username",$scope.user.username);
              $cookies.put("zdwq_password",$scope.user.password);
            }
            else{
              if($cookies.get("zdwq_username")) $cookies.remove("zdwq_username");
              if($cookies.get("zdwq_password")) $cookies.remove("zdwq_password");
            }
            $window.sessionStorage.setItem("haslogined",true) // 不关闭页面的情况下,刷新页面状态不变
            alert("登录成功");
            $rootScope.isUserAuth = true;
          },function(){
            alert("登录失败")
          })
        }).error(function(){
          // alert("请输入正确的账号或密码")
        })
      }

    (2)  input 中指定validator,表示使用哪个验证规则,另外input中可指定valid-method 表示采用哪种方式对input使用规则,有watch,blur,submit,submit-only方式,默认是watch。需要在点击登录按钮验证的时候,需要指定submit,submit-only 中的一种方式,但是需要为input指定name(没有限制名字的要求)  .为了取得好看的效果,我制作的页面中使用了blur与submit的组合方式。

angular validation 使用总结的更多相关文章

  1. [Angular2 Form] Style Validation in Angular 2 Forms

    Inputs using Angular 2’s ngModel automatically apply style classes of .ng-validand .ng-invalid each ...

  2. [Angular2 Form] Display Validation and Error Messaging in Angular 2

    Angular 2’s ngModel provides error objects for each of the built-in input validators. You can access ...

  3. [Angular 2] Validation

    Define a filed should has validation: export class DemoFormSku { myForm: ControlGroup; sku: Abstract ...

  4. [Angular2 Form] Create Radio Buttons for Angular 2 Forms

    Using Radio Buttons in Angular 2 requires a basic understanding of forms as well as how their labels ...

  5. angular : ngModel 内部流程

    angular 1.5 beta link NgModelController provides API for the ngModel directive. The controller conta ...

  6. 创建自定义的 Angular Schematics

    本文对 Angular Schematics 进行了介绍,并创建了一个用于创建自定义 Component 的 Schematics ,然后在 Angular 项目中以它为模板演练了通过 Schemat ...

  7. [转]Angular: Hide Navbar Menu from Login page

    本文转自:https://loiane.com/2017/08/angular-hide-navbar-login-page/ In this article we will learn two ap ...

  8. Angular 2 to Angular 4 with Angular Material UI Components

    Download Source - 955.2 KB Content Part 1: Angular2 Setup in Visual Studio 2017, Basic CRUD applicat ...

  9. User Authentication with Angular and ASP.NET Core

    User authentication is a fundamental part of any meaningful application. Unfortunately, implementing ...

随机推荐

  1. Myloader参数说明

    -d, --directory 备份文件的目录 -q, --queries-per-transaction 每次事务执行的查询数量,默认是1000 -o, --overwrite-tables 如果要 ...

  2. export / import 温故而知新

    认知一: 导出的对象被修改,依然会影响原来的对象. 仔细想想这是理所当然的事(说明导出的依然是对象指向内存的地址,所以通常还需要结合深拷贝使用) /** export const state = { ...

  3. Spring开发工具Spring Tools Suite(STS)

    The Spring Tool Suite is an Eclipse-based development environment that is customized for developing ...

  4. Communications link failure due to underlying exception异常处理(转)

    最近的一个项目在Hibernate使用C3P0的连接池,数据库为Mysql.开发测试没有问题,在运行中每个一段长的空闲时间就出现异常: java 代码 org.hibernate.exception. ...

  5. ASP.NET CORE的H5上传

    做的CORE项目中用到H5上传,把以前的MVC代码复制过来得修改一下才能用在.NET CORE中

  6. 【Tomcat】面向初级 Web 开发人员的 Tomcat

    Apache Tomcat 应用服务器不再是高级 Web 系统开发人员的专用领域.在本教程中,Sing Li 将向初级 Web 开发人员展示如何利用他们当前的 Java™ 开发技能,使用 Tomcat ...

  7. 【论文笔记】使用SPSS 进行 T Test (T检验)

    从具有t值来看,你是在进行T检验.T检验是平均值的比较方法. T检验分为三种方法: 1. 单一样本t检验(One-sample t test),是用来比较一组数据的平均值和一个数值有无差异.例如,你选 ...

  8. 安装Node和NPM

      1.node和NPM是什么?   Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js ...

  9. 【css】css 中文字体 unicode 对照表

    css 中文字体可以用 unicode 格式来表示,比如“宋体”可以用 \5B8B\4F53 来表示.具体参考下表: 中文名 英文名 unicode 宋体 SimSun \5B8B\4F53 黑体 S ...

  10. redis Sentinel部署

    sentinel 系统用于管理多个 Redis 服务器(instance) 执行以下三个任务: 监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常. 提醒 ...