其他分享
首页 > 其他分享> > angularjs 验证用户输入的值是否符合Hex颜色

angularjs 验证用户输入的值是否符合Hex颜色

作者:互联网

正则:

/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/


 

'use strict';

dmApp.directive('validateHexColor', function () {

    var REQUIRED_PATTERNS = [
        /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/
    ];

    return {
        require: 'ngModel',
        link: function ($scope, element, attributes, ngModelController) {

            ngModelController.$validators.validateHexColor = function (modelValue, viewValue) {
                var status = true;
                if (!ngModelController.$isEmpty(modelValue)) {
                    angular.forEach(REQUIRED_PATTERNS, function (pattern) {
                        status = status && pattern.test(modelValue);
                    });
                }
                return status;
            };
        }
    };
});
js Source Code

 

如:

 

标签:f0,status,验证,function,Hex,Fa,angularjs,modelValue,ngModelController
来源: https://www.cnblogs.com/insus/p/13515622.html