我想验证一个URL并显示消息.以下是我的代码:
$("#pageUrl").keydown(function(){
$(".status").show();
var url = $("#pageUrl").val();
if(isValidURL(url)){
$.ajax({
type: "POST",
url: "demo.php",
data: "pageUrl="+ url,
success: function(msg){
if(msg == 1 ){
$(".status").html('SiteID:12345678901234456');
}else{
$(".status").html('');
}
}
});
}else{
$(".status").html('');
}
});
function isValidURL(url){
var RegExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
if(RegExp.test(url)){
return true;
}else{
return false;
}
}
我的问题是,即使输入正确的URL直到它与正则表达式匹配,它也会显示错误消息,即使URL类似,它也会返回true "http://wwww"
.
我感谢你的建议.
有人提到了Jquery Validation插件,如果你只是想验证url似乎有点过分,这里是插件的正则表达式:
return this.optional(element) || /^(https?|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);
以下是他们从中获取的地方:http://projects.scottsplayground.com/iri/
由@nhahtdh指出这已更新为:
// Copyright (c) 2010-2013 Diego Perini, MIT licensed // https://gist.github.com/dperini/729294 // see also https://mathiasbynens.be/demo/url-regex // modified to allow protocol-relative URLs return this.optional( element ) || /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?)(?::\d{2,5})?(?:[/?#]\S*)?$/i.test( value );
来源:https://github.com/jzaefferer/jquery-validation/blob/c1db10a34c0847c28a5bd30e3ee1117e137ca834/src/core.js#L1349
使用正则表达式解析URL是不切实际的.RFC1738规则的完整实现将导致极长的正则表达式(假设它甚至可能).当然,您当前的表达式会失败许多有效的URL,并传递无效的URL.
代替:
一个.使用实际遵循真实规则的正确URL解析器.(我不知道有一个用于JavaScript;它可能是矫枉过正的.你可以在服务器端做到这一点).要么,
湾 只需删除任何前导或尾随空格,然后检查它在前面有一个首选方案(通常是"http://"或"https://"),并保留它.要么,
C.尝试使用URL并查看最后的内容,例如通过从服务器端发送HTTP HEAD请求.如果您收到404或连接错误,则可能是错误的.
即使网址类似于" http:// wwww ",它也会返回true .
嗯,这确实是一个完全有效的URL.
如果要检查"wwww"等主机名是否确实存在,则别无选择,只能在DNS中查找.同样,这将是服务器端代码.
function validateURL(textval) { var urlregex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/; return urlregex.test(textval); }
对于以下URL,这可以返回true:
http://stackoverflow.com/questions/1303872/url-validation-using-javascript
要么:
http://regexlib.com/DisplayPatterns.aspx?cattabindex=1&categoryId=2
我还写了一个基于rfc1738和rfc3986的URL验证功能来检查http和https网址.我尝试保持这种模块化,因此可以更好地维护并适应自己的要求.
一行中的RegExp显示在本文末尾.
RegExp接受带有一些国际域或IPv4号码的HTTP和HTTPS URL.目前还不支持IPv6.
window.isValidURL = (function() {// wrapped in self calling function to prevent global pollution //URL pattern based on rfc1738 and rfc3986 var rg_pctEncoded = "%[0-9a-fA-F]{2}"; var rg_protocol = "(http|https):\\/\\/"; var rg_userinfo = "([a-zA-Z0-9$\\-_.+!*'(),;:&=]|" + rg_pctEncoded + ")+" + "@"; var rg_decOctet = "(25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|[1-9][0-9]|[0-9])"; // 0-255 var rg_ipv4address = "(" + rg_decOctet + "(\\." + rg_decOctet + "){3}" + ")"; var rg_hostname = "([a-zA-Z0-9\\-\\u00C0-\\u017F]+\\.)+([a-zA-Z]{2,})"; var rg_port = "[0-9]+"; var rg_hostport = "(" + rg_ipv4address + "|localhost|" + rg_hostname + ")(:" + rg_port + ")?"; // chars sets // safe = "$" | "-" | "_" | "." | "+" // extra = "!" | "*" | "'" | "(" | ")" | "," // hsegment = *[ alpha | digit | safe | extra | ";" | ":" | "@" | "&" | "=" | escape ] var rg_pchar = "a-zA-Z0-9$\\-_.+!*'(),;:@&="; var rg_segment = "([" + rg_pchar + "]|" + rg_pctEncoded + ")*"; var rg_path = rg_segment + "(\\/" + rg_segment + ")*"; var rg_query = "\\?" + "([" + rg_pchar + "/?]|" + rg_pctEncoded + ")*"; var rg_fragment = "\\#" + "([" + rg_pchar + "/?]|" + rg_pctEncoded + ")*"; var rgHttpUrl = new RegExp( "^" + rg_protocol + "(" + rg_userinfo + ")?" + rg_hostport + "(\\/" + "(" + rg_path + ")?" + "(" + rg_query + ")?" + "(" + rg_fragment + ")?" + ")?" + "$" ); // export public function return function (url) { if (rgHttpUrl.test(url)) { return true; } else { return false; } }; })();
RegExp在一行:
var rg = /^(http|https):\/\/(([a-zA-Z0-9$\-_.+!*'(),;:&=]|%[0-9a-fA-F]{2})+@)?(((25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|[1-9][0-9]|[0-9])(\.(25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|[1-9][0-9]|[0-9])){3})|localhost|([a-zA-Z0-9\-\u00C0-\u017F]+\.)+([a-zA-Z]{2,}))(:[0-9]+)?(\/(([a-zA-Z0-9$\-_.+!*'(),;:@&=]|%[0-9a-fA-F]{2})*(\/([a-zA-Z0-9$\-_.+!*'(),;:@&=]|%[0-9a-fA-F]{2})*)*)?(\?([a-zA-Z0-9$\-_.+!*'(),;:@&=\/?]|%[0-9a-fA-F]{2})*)?(\#([a-zA-Z0-9$\-_.+!*'(),;:@&=\/?]|%[0-9a-fA-F]{2})*)?)?$/;
在类似的情况下,我逃避了这个:
someUtils.validateURL = function(url) { var parser = document.createElement('a'); try { parser.href = url; return !!parser.hostname; } catch (e) { return false; } };
也就是说,如果浏览器可以为你做这个发明轮子?但是,当然,这只能在浏览器中使用.
解析URL的各个部分正是浏览器如何解释它:
parser.protocol; // => "http:" parser.hostname; // => "example.com" parser.port; // => "8080" parser.pathname; // => "/path/" parser.search; // => "?search=test" parser.hash; // => "#hash" parser.host; // => "example.com:3000"
使用这些可以根据要求改进验证功能.唯一的缺点是它将接受相对URL并使用当前页面服务器的主机和端口.但是您可以通过重新组装部件中的URL并将其全部传递给您的AJAX服务来使用它.
什么validateURL
也不会接受无效的网址,例如http:\:8883
将返回false,但是:1234
是有效的,被解释为http://pagehost.example.com/:1234
即作为相对路径.
UPDATE
此方法不再适用于Chrome和其他WebKit浏览器.即使URL无效,主机名也会填充一些值,例如取自base
.它仍然有助于解析部分URL,但不允许验证URL.
可能更好的无自己解析器方法是使用var parsedURL = new URL(url)
和捕获异常.请参阅例如URL API.所有主流浏览器和NodeJS都支持,虽然仍然标有实验性.