gpt4 book ai didi

validation - 覆盖 AngularJS URL 验证器

转载 作者:行者123 更新时间:2023-12-04 09:28:45 25 4
gpt4 key购买 nike

AngularJS 接受这个有效的 URL:

var URL_REGEXP = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/;

Django 接受这个:
regex = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
r'localhost|' #localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)

实用 不同之处在于 AngularJS 接受 http://some-host-without-tld , 而 Django 只允许 http://localhost作为没有 TLD 的有效 URL。

由于覆盖 Django URL Validator 有问题,我想覆盖 AngularJS 验证器。我以这种方式尝试过,它有效:
app.overwriteUrlValidator = function(ngModel) {

// Django RegExp, ported to JS.
var URL_REGEXP = /^(?:http|ftp)s?:\/\/(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|localhost|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?::\d+)?(?:\/?|[\/?]\S+)$/gi;

// Same validator as AngularJS's, only with a different RegExp.
function urlValidator(value) {
if (ngModel.$isEmpty(value) || URL_REGEXP.test(value)) {
ngModel.$setValidity('url', true);
return value;
} else {
ngModel.$setValidity('url', false);
return undefined;
}
}

// This is the part I'm not happy about. I need to use a timeout
// because my code is executed before Angular adds its URL validator.
// If I add mine before Angular does, it will not work for ??? reason.
window.setTimeout(function() {
ngModel.$formatters.push(urlValidator);
ngModel.$parsers.push(urlValidator);
}, 100);
};


/**
* Keep track of user's interaction with input fields
*/
app.inputDirective = function() {
function link(scope, element, attrs, ngModel) {
if (ngModel && attrs.type === 'url') {
app.overwriteUrlValidator(ngModel);
}
}

return {
restrict: 'A',
require : '?ngModel',
link : link
};
};
app.directive('input', app.inputDirective);
app.directive('textarea', app.inputDirective);

我宁愿不切换到另一个验证指令,因为我必须更新和检查大量代码。

有谁知道这样做的可靠方法?

最佳答案

从 Angular 1.3 开始,现在你可以完全覆盖现有的 $validators 相对容易。 :

myApp.directive('input', function() {
function link(scope, element, attrs, ngModel) {
function allowSchemelessUrls() {
// Match Django's URL validator, which allows schemeless urls.
var URL_REGEXP = /^((?:http|ftp)s?:\/\/)(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|localhost|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?::\d+)?(?:\/?|[\/?]\S+)$/i;

// Silently prefixes schemeless URLs with 'http://' when
// converting a view value to model value.
ngModel.$parsers.unshift(function(value) {
if (!URL_REGEXP.test(value) && URL_REGEXP.test('http://' + value)) {
return 'http://' + value;
} else {
return value;
}
});

ngModel.$validators.url = function(value) {
return ngModel.$isEmpty(value) || URL_REGEXP.test(value);
};
}

if (ngModel && attrs.type === 'url') {
allowSchemelessUrls();
}
}

return {
require: '?ngModel',
link: link
};
});

关于validation - 覆盖 AngularJS URL 验证器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21138574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com