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?
-
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
6 Answers
Reset to default 2Try 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.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?");
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.