Reputation: 231
I need to validate the url using regex. The valid formats that should be accepted are as follows:
users.kathir.com
www.kathir.com
I am currently using the following regex:
^[a-z0-9-]+(\.[a-z0-9-])+(.[a-z])
But it accepts www.google as the valid one, but it should accept either google.com or www.google.com only. Please suggest
Upvotes: 1
Views: 8313
Reputation: 3463
The answer provided by user will also validate those regex in which there is no .com|edu|org etc at the end to make it more appropriate I have modify the regex which works fine for me and also want to share with you
var pattern = /^(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])+(.[a-z])?/;
var regex = new RegExp(pattern);
var website_url = $("#website_url").val();// url of the website
if (website_url.match(regex)) {
return true
} else {
return false;
}
if you like it do an Upvote
Upvotes: 1
Reputation: 4575
I use this, works pretty well:
function checkUrl(url){
return url.match(/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/);
}
Hope that helps :)
Upvotes: 2