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>

    运行结果:

  • 相关阅读:
    URL中的特殊字符 + % # & = ? /
    mvc 前端显示文本中的html标签处理
    URL中#号(井号)的作用
    自定义配置节 Section
    vshost.exe.config与.exe.config ;Debug目录与Release目录;bin目录与obj目录
    在MVC过滤器中获取触发的Controller、Action、参数 等
    js设置全局变量 ajax中赋值
    批处理 安装、卸载 window service
    [摘]HttpContext, HttpRequest, HttpResponse, HttpRuntime, HttpServerUtility
    摘要:ASP.NET的路由
  • 原文地址:https://www.cnblogs.com/gylhaut/p/5234513.html
Copyright © 2011-2022 走看看