无法了解为什么jQuery验证插件的URL自定义验证方法不起作用 [英] Can't get why URL custom validation method for jQuery validation plugin not working
问题描述
我正在尝试验证URL.我想使这些URL通过验证:
I'm trying to validate URL. I want to make these URLs pass the validation:
www.site.com
www.super.co.uk
我正在此站点上检查我的RegEx- http://www.rubular.com/
I'm checking my RegEx in this site - http://www.rubular.com/
这是我的RegEx:
/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
它在Rubular网站上正常工作,但是当我尝试在我的自定义jQuery验证插件方法中使用它时-它不断告诉我,它是无效的:
It is working in Rubular site, but when I'm trying to use it in my custom jQuery validation Plugin method - it constantly tells me, that it is invalid:
jQuery.validator.addMethod("complete_url", function(val, elem) {
// if no url, don't do anything
if (val.length == 0) { return true; }
return /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/.test(val);
});
...
rules: {
'website[url]': {
required: true,
url: "complete_url",
remote: {
url: "http://127.0.0.1:3000/checkurl"
//url: "http://91.228.126.168:3000/checkurl"
}
}
}
推荐答案
/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/.test("www.super.co.uk");
在chrome的控制台中返回true,因此可能与您的插件有关.
/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/.test("www.super.co.uk");
returns true in chrome's console, so it is probably something with your plugin.
这篇关于无法了解为什么jQuery验证插件的URL自定义验证方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!