kalles
kalles

Reputation: 337

Validate URL without any http , https or ftp

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?

Upvotes: 1

Views: 8219

Answers (6)

Rex. A
Rex. A

Reputation: 509

Match Regex without (http|https|www)

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

This regex will work for

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

This regex will not work for

    www.google.com
    http://google.com
    http://www.google.com

Upvotes: 0

zennkaiii
zennkaiii

Reputation: 74

Regex for URL without protocol

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

Try it here

Upvotes: 0

Shubham Khatri
Shubham Khatri

Reputation: 281950

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

Upvotes: 0

derlarsschneider
derlarsschneider

Reputation: 296

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.

Upvotes: 0

Maths RkBala
Maths RkBala

Reputation: 2195

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.com
http://example.com
ftp://example.com
www.example.com
https://www.example.com
http://www.example.com
ftp://www.example.com
example.com

Upvotes: 2

Jayanti Lal
Jayanti Lal

Reputation: 1185

I hope this will help

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

or

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

Upvotes: 0

Related Questions