用于验证 URL 的 Jquery 验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16537628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:19:14  来源:igfitidea点击:

Jquery Validation to validate URL

jqueryregexjquery-validate

提问by Roopendra

I am trying to validate URL in jQuery regular expressionusing below code. It is working fine with http://wwwand https://www

我正在尝试regular expression使用以下代码验证 jQuery 中的 URL 。它与http://wwwhttps://www

var myVariable = "http://www.example.com/2013/05/test-page-url-512-518.html";

if(/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/|www\.)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/.test(myVariable)){
    alert("valid url");
} else {
    alert("invalid url");
}

Edit:-

编辑:-

Above code work perfectly to validate URL. That time my requirement was only to validate http://wwwand https://www

上面的代码可以完美地验证 URL。那个时候我的要求只是验证http://wwwhttps://www

采纳答案by Chu Ya'an Xiaonan

your pattern is defined as

你的模式被定义为

^(http:\/\/|someting else

thus any-urls begin with "http://" would be validated.

因此任何以“http://”开头的网址都将被验证。

回答by ChamingaD

Check this - http://docs.jquery.com/Plugins/Validation/Methods/url

检查这个 - http://docs.jquery.com/Plugins/Validation/Methods/url

$("#myform").validate({
  rules: {
    field: {
      required: true,
      url: true
    }
  }
});

回答by Santosh Panda

You can change your Regex, actually there is a repetition of item i.e. http:\/\/|https:\/\/|www\.

你可以改变你的正则表达式,实际上有一个项目的重复,即 http:\/\/|https:\/\/|www\.

This code will work out for you:

此代码将为您解决:

var myVariable = "http://www.example.com/2013/05/test-page-url-512-518.html";

if(/^(http:\/\/www\.|https:\/\/www\.)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/.test(myVariable)){
    alert("valid url");
} else {
    alert("invalid url");
}

回答by Vishnu Sharma

You want to validate your URL:

您想验证您的网址:

Please pass the URL in this function then this will give you true OR false.

请在此函数中传递 URL,然后这将为您提供 true 或 false。

The Function :

功能 :

<script>
function isUrl(s) {
    var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    return regexp.test(s);
}

isUrl(yourURL);
</script>

回答by Ashok Damani

try this--->

试试这个--->

var myVariable = "http://www.Test.com/";
if(/^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!$&'\(\)\*\+,;=]|:)*@)?((\[(|(v[\da-f]{1,}\.(([a-z]|\d|-|\.|_|~)|[!$&'\(\)\*\+,;=]|:)+))\])|((\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])|(%[\da-f]{2})|[!$&'\(\)\*\+,;=])*)(:\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})|[!$&'\(\)\*\+,;=]|:|@)*)*)?)|((([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})|[!$&'\(\)\*\+,;=]|:|@)){0})(\?((([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(myVariable)) {
  alert("valid url");
} else {
  alert("invalid url");
}