简体   繁体   English

jQuery验证在有效URL上失败

[英]jquery validation failing on valid URLs in the wild

I am using Jquery validation to validate a URL: http://docs.jquery.com/Plugins/Validation/Methods/url 我正在使用Jquery验证来验证URL: http : //docs.jquery.com/Plugins/Validation/Methods/url

I am finding the regex jquery uses to not be a good one for URLs in the wild. 我发现正则表达式jquery并不是野外URL的好选择。 It may perfect according to some RFC however browsers and servers are not following RFCs to the letter which is not unusual of course. 根据某些RFC可能会很完美,但是浏览器和服务器并未遵循RFC的规定,这当然并不罕见。 It looks like the jquery validation regex needs improvements. 看来jquery验证正则表达式需要改进。

So far I have found the jquery URL validator incorrectly marks URLs with { or } characters as invalid. 到目前为止,我发现jquery URL验证程序错误地将带有{或}字符的URL标记为无效。 However these characters work fine in popular browsers and are used on popular web sites. 但是,这些字符在流行的浏览器中可以正常工作,并在流行的网站上使用。

Here is an example of a URL in the wild that works fine however it fails jquery URL validation: 这是一个狂野的URL的示例,可以正常工作,但是无法通过jquery URL验证:

http://www.bestbuy.com/site/amazon-kindle-fire-hd-7-inch-tablet-with-8gb-memory-2nd-generation-black/2216082.p?id=1219070629225&skuId=2216082&ref=06&loc=01&ci_src=14110944&ci_sku=2216082&extensionType={adtype}:{network}&s_kwcid=PTC!pla!{keyword}!{matchtype}!{adwords_producttargetid}!{network}!{ifmobile:M}!{creative}&kpid=2216082&k_clickid=046dfb11-a86b-b669-52e3-00004228f299

Jquery validation uses the following regex to validate URLs: jQuery验证使用以下正则表达式来验证URL:

url: function( value, element ) {

// contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/

        return this.optional(element) || /^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);

}

Does anybody know a more better way to validate URLs in javascript or better regex that can handle real world URLs? 有谁知道一种更好的方法来验证javascript中的URL或可以处理现实URL的更好的正则表达式?

I ended up solving this by creating a wrapper for the jquery url validation that URI encodes special characters. 最后,我通过为URI编码特殊的jQuery URL验证创建包装器来解决了这个问题。

$(function() { 
    "use strict";

    var urlFunctionPtr = $.validator.methods["url"];
    $.validator.methods["url"] = function() {
        if(arguments.length > 0) {
            arguments[0] = encodeURI(arguments[0]);
        }

        return urlFunctionPtr.apply(this, arguments);
    };
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM