zoukankan      html  css  js  c++  java
  • ng-messages AngularJs 表单校验方式

    最新研究了一下表单校验,直接上代码。

    <!DOCTYPE html>
    <html ng-app='app'>
    <head>
    <meta charset="utf-8">
    <title translate="TITLE">Basic usage</title>
    <link href="../css/bootstrap.css" rel="stylesheet" />
    <style>body { text-align: center;
    padding-top: 30px;
    }
    </style>
    <script type="text/ng-template" id="form-messages">
    <div ng-message="required">This field cannot be blank</div>
    <div ng-message="minlength">The value for this field is too short</div>
    <div ng-message="maxlength">The value for this field is too long</div>
    <div ng-message="email">You have entered an incorrect email value</div>
    <div ng-message="pattern">You did not enter the value in the correct format</div>
    <div ng-message="password-characters">
    Your password must consist of alphabetical or numeric characters.
    It must also contain at least one special character, a number as well as an uppercase letter.
    </div>
    </script>
    <script type="text/javascript" src="../script/angular.js"></script>
    <script type="text/javascript" src="../script/angular-messages.js"></script>
    <script type="text/javascript">
    angular.module('app', ['ngMessages'])
    .controller('FormCtrl', function($scope) {

    })
    .directive('matchValidator', function() {
    return {
    require: 'ngModel',
    link : function(scope, element, attrs, ngModel) {
    ngModel.$parsers.push(function(value) {
    ngModel.$setValidity('match', value == scope.$eval(attrs.matchValidator));
    return value;
    });
    }
    }
    })
    .directive('passwordCharactersValidator', function() {
    var PASSWORD_FORMATS = [
    /[^ws]+/, //special characters
    /[A-Z]+/, //uppercase letters
    /w+/, //other letters
    /d+/ //numbers
    ];

    return {
    require: 'ngModel',
    link : function(scope, element, attrs, ngModel) {
    ngModel.$parsers.push(function(value) {
    var status = true;
    angular.forEach(PASSWORD_FORMATS, function(regex) {
    status = status && regex.test(value);
    });
    ngModel.$setValidity('password-characters', status);
    return value;
    });
    }
    }
    })
    </script>
    </head>

    <body >
    <!-- ng-controller="FormCtrl" ng-submit="submit()" ng-if="interacted(my_form.password)"-->
    <form name="my_form" class="main-form container" ng-controller="FormCtrl" novalidate>
    <div class="control-group">
    <label for="input_password">Create a password:</label>
    <input class="form-control"
    type="password"
    name="password"
    id="input_password"
    ng-model="data.password"
    ng-minlength="6"
    ng-maxlength="12"
    password-characters-validator
    required />

    <div

    ng-messages="my_form.password.$error"
    ng-messages-include="form-messages"></div>
    </div>
    <div class="control-group">
    <label for="input_password_confirm">Confirm your password:</label>
    <input class="form-control"
    type="password"
    name="password_confirm"
    id="input_password_confirm"
    ng-model="data.password_confirm"
    ng-minlength="6"
    ng-maxlength="12"
    password-characters-validator
    match-validator="data.password"
    required />
    <!--ng-if="interacted(my_form.password_confirm)" -->
    <div ng-messages="my_form.password_confirm.$error" ng-messages-include="form-messages">
    <div ng-message="match">This password does not match the password entered before</div>
    </div>
    </div>
    </form>

    </body>
    </html>

    运行结果:

  • 相关阅读:
    2020/3/21 简单的学习
    2020/3/7 A-B
    2020/3/6 旋转骰子
    2020/3/6 美丽数组
    面向对象程序设计寒假作业2
    自我介绍
    深度优先搜索-迷宫问题(走迷宫题解)
    开机方案题解
    好吃的巧克力题解
    数楼梯题解
  • 原文地址:https://www.cnblogs.com/gylhaut/p/5234513.html
Copyright © 2011-2022 走看看