• 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>

    运行结果:

  • 相关阅读:
    【XR-4】文本编辑器
    二十四、JMeter实战-Linux下搭建JMeter + Ant + Jenkins自动化框架
    Python 接口自动化
    Docker 部署 Tomcat
    CentOS7 查看 ip 地址
    Python
    Python接口自动化 -RESTful请求方法封装
    Python接口自动化
    Python
    xcode 编译webdriveragent
  • 原文地址:https://www.cnblogs.com/gylhaut/p/5234513.html
Copyright © 2020-2023  润新知