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

javascript - Validate URL without any http , https or ftp - Stack Overflow

programmeradmin1浏览0评论

I need to validate a url.

var urlPattern = new RegExp("(http|ftp|https)://[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?");

If the URL have http,https or ftp i can validate. Incase if that URL does not have http,ftp or https means how can i validate?

I need to validate a url.

var urlPattern = new RegExp("(http|ftp|https)://[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?");

If the URL have http,https or ftp i can validate. Incase if that URL does not have http,ftp or https means how can i validate?

Share Improve this question edited Apr 30, 2016 at 15:00 Alan Moore 75.3k13 gold badges107 silver badges161 bronze badges asked Apr 18, 2016 at 9:15 kalleskalles 3372 gold badges6 silver badges22 bronze badges 1
  • If you are happy your current regexp is correct, but you just want to also validate without any protocol, you can just remove the protocol part: (http|ftp|https)://? – Don't Panic Commented Apr 18, 2016 at 10:47
Add a ment  | 

6 Answers 6

Reset to default 2

Try the following:

var regex = new RegExp("^(http[s]?:\\/\\/(www\\.)?|ftp:\\/\\/(www\\.)?|www\\.){1}([0-9A-Za-z-\\.@:%_\+~#=]+)+((\\.[a-zA-Z]{2,3})+)(/(.)*)?(\\?(.)*)?"); 
var without_regex = new RegExp("^([0-9A-Za-z-\\.@:%_\+~#=]+)+((\\.[a-zA-Z]{2,3})+)(/(.)*)?(\\?(.)*)?");
var str = "url";
if(regex.test(str) || without_regex.test(str)){
    alert("Successful");
}else{
    alert("No match");
}

It accepts the following urls:

https://example.
http://example.
ftp://example.
www.example.
https://www.example.
http://www.example.
ftp://www.example.
example.

I hope this will help

 var url = 'http://www.Jamboo.';
 var correctUrl= /^(ftp|http|https):\/\/[^ "]+$/.test(url);
  // true

or

  var r = /^(ftp|http|https):\/\/[^ "]+$/;
  r.test('http://www.Jam  boo.');
   // false

Looking at the regex of @Shubham Khatri I would suggest (if possible, of course) to use external tools like wget or curl to query the address and check the result.

Regex:

/(?(?:(http|https|ftp)://)?(?:((?:[^\W\s]|.|-|[:]{1})+)@{1})?((?:www.)?(?:[^\W\s]|.|-)+[.][^\W\s]{2,4}|localhost(?=/)|\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})(?::(\d*))?([/]?[^\s\?][/]{1})(?:/?([^\s\n\?[]{}#](?:(?=.)){1}|[^\s\n\?[]{}.#])?([.]{1}[^\s\?#])?)?(?:\?{1}([^\s\n#[]]))?([#][^\s\n]*)?)?/g

HTML

<input type= text id = 'url'>

JS

$('#url').on('blur', function() {
  var val = $(this).val();
  if(val.match(/\(?(?:(http|https|ftp):\/\/)?(?:((?:[^\W\s]|\.|-|[:]{1})+)@{1})?((?:www.)?(?:[^\W\s]|\.|-)+[\.][^\W\s]{2,4}|localhost(?=\/)|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?::(\d*))?([\/]?[^\s\?]*[\/]{1})*(?:\/?([^\s\n\?\[\]\{\}\#]*(?:(?=\.)){1}|[^\s\n\?\[\]\{\}\.\#]*)?([\.]{1}[^\s\?\#]*)?)?(?:\?{1}([^\s\n\#\[\]]*))?([\#][^\s\n]*)?\)?/g) != null)
      alert('success');
})

DEMO

Alternately use:

$('#url').on('blur', function() {
    var urlPattern = new RegExp("((http|ftp|https)://)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:/~+#-]*[\w@?^=%&amp;/~+#-])?");
  var val = $(this).val();
  if(urlPattern.test(val)) {
    alert('success');
  } else {
    alert('fail')
  }
})

DEMO

Regex for URL without protocol

/^[\w.-]+(?:\.[\w\.-]+)+[\w\-\._%~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/

Try it here

Match Regex without (http|https|www)

    /^(?!https?)(?!www\.?).*\..+$/g

This regex will work for

    google.
    mail.google.
    app.firebase.google.
    google./something

This regex will not work for

    www.google.
    http://google.
    http://www.google.
发布评论

评论列表(0)

  1. 暂无评论