zoukankan      html  css  js  c++  java
  • AngularJS自定义表单验证器

    <!doctype html>
    <html ng-app="myApp">
    <head>
        <script src="G:\Source\Repos\GWD\Gridsum.WebDissector.Website.ZC\Gridsum.WebDissector.Website.ZC\Pages\dist\assets\lib\angularjs\angular.js"></script>
        <script type="text/javascript">
            var app = angular.module('myApp', []);
            var INTEGER_REGEXP = /^-?d*$/;
            app.directive('integer', function () {
                return {
                    require: 'ngModel',
                    link: function (scope, elm, attrs, ctrl) {
                        ctrl.$parsers.unshift(function (viewValue) {
                            if (INTEGER_REGEXP.test(viewValue)) {
                                // it is valid
                                ctrl.$setValidity('integer', true);
                                return viewValue;
                            } else {
                                // it is invalid, return undefined (no model update)
                                ctrl.$setValidity('integer', false);
                                return undefined;
                            }
                        });
                    }
                };
            });
            var FLOAT_REGEXP = /^-?d+((.|\,)d+)?$/;
            app.directive('smartFloat', function () {
                return {
                    require: 'ngModel',
                    link: function (scope, elm, attrs, ctrl) {
                        ctrl.$parsers.unshift(function (viewValue) {
                            if (FLOAT_REGEXP.test(viewValue)) {
                                ctrl.$setValidity('float', true);
                                return parseFloat(viewValue.replace(',', '.'));
                            } else {
                                ctrl.$setValidity('float', false);
                                return undefined;
                            }
                        });
                    }
                };
            });
        </script>
    </head>
    <body>
        <div>
            <form name="form" class="css-form" novalidate>
                <div>
                    Size (integer 0 - 10):
                    <input type="number" ng-model="size" name="size"
                           min="0" max="10" integer />
                    <span ng-show="form.size.$error.integer">This is not valid integer!</span>
                    <span ng-show="form.size.$error.min || form.size.$error.max">
                        The value must be in range 0 to 10!
                    </span>
                </div>
                <div>
                    Length (float):
                    <input type="text" ng-model="length" name="length" smart-float />
                    <span ng-show="form.length.$error.float">
                        This is not a valid float number!
                    </span>
                </div>
            </form>
        </div>
    </body>
    </html>
    

      

  • 相关阅读:
    SDN第三次上机作业
    团队作业——Beta冲刺
    SDN第二次上机作业
    在mpvue中使用map如何避坑
    仿一个好玩的滑动效果
    乡音
    mpvue支持小程序的分包加载
    台风🌀和口腔溃疡
    记一次cocos项目的加载速度优化
    如何用ajax下载文件
  • 原文地址:https://www.cnblogs.com/chengshuiqiang/p/4623208.html
Copyright © 2011-2022 走看看