最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript URL validation regex - Stack Overflow

programmeradmin8浏览0评论

I had the following url validation regex:

/(ftp|https?)://[^ "]+$/

This is from the ref: Regular expression for URL validation (in JavaScript)

This works fine including cases like http://localhost:8080, but it also validates the below ones that i dont want. The above regex should not pass the below urls

 1. .
 2. http://
 3. http://.
 4. . 

Kindly help as am a noob in regex

Second question, though not related to the question but regex is, when i validate null and undefined against the regex /^[a-z]+$/i i get true. Is this the default behavior or am i missing something over here?

I had the following url validation regex:

/(ftp|https?)://[^ "]+$/

This is from the ref: Regular expression for URL validation (in JavaScript)

This works fine including cases like http://localhost:8080, but it also validates the below ones that i dont want. The above regex should not pass the below urls

 1. http://www.example..
 2. http://.
 3. http://.
 4. http://www.example. 

Kindly help as am a noob in regex

Second question, though not related to the question but regex is, when i validate null and undefined against the regex /^[a-z]+$/i i get true. Is this the default behavior or am i missing something over here?

Share Improve this question edited May 23, 2017 at 12:02 CommunityBot 11 silver badge asked Aug 21, 2013 at 17:52 hellojavahellojava 5,06410 gold badges32 silver badges38 bronze badges 15
  • what do you mean by fail? are they marked as valid or invalid? – Philipp Sander Commented Aug 21, 2013 at 17:57
  • check this - stackoverflow./a/8234912/1823389 – Moazzam Khan Commented Aug 21, 2013 at 17:59
  • 2 This has been so beat to death. Please, PLEASE, search before asking questions. – coreyward Commented Aug 21, 2013 at 18:00
  • possible duplicate of regular expression for url – coreyward Commented Aug 21, 2013 at 18:00
  • possible duplicate of url validation regex – coreyward Commented Aug 21, 2013 at 18:00
 |  Show 10 more ments

3 Answers 3

Reset to default 8

Try this

function is_valid_url(url)
{
     return url.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/);
}

Call the is_valid_url(YOUR_WEBSITE_URL) function everywhere which you want to website validate.

Try This ..

<script src="http://code.jquery./jquery-1.11.1.min.js"></script>
<script src="http://jqueryvalidation/files/dist/jquery.validate.min.js"></script>
<script src="http://jqueryvalidation/files/dist/additional-methods.min.js"></script>
<script>
// just for the demos, avoids form submit
jQuery.validator.setDefaults({
  debug: true,
  success: "valid"
});
$( "#myform" ).validate({
  rules: {
    field: {
      required: true,
      url: true
    }
  }
});
</script>

I built this regex IP and DNS validation for javascript.

/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?((([a-z0-9])*[:]){1}|(([a-z0-9])*[.]){1}|(([0-9]){1,3}([.]|[:])){3}(([0-9]){1,3}[:]){1})([a-z]{2,5}|[0-9]{1,5})([.]([a-z0-9]){1,3})?(\/.*)?$/
发布评论

评论列表(0)

  1. 暂无评论