Regex to block domains in url field - javascript

I want to block certain domains from input url field, i'm using jQuery Validation plugin, i've created a custom method only thing I can't figure out is the regex, it is driving me crazy.
$.validator.addMethod("customurl",
function(value, element) {
return /^([\w-.]+#(?!facebook\.com)(?!twitter\.com)(?!instagram\.com)([\w-]+.)+[\w-]{2,4})?$/.test(value);
},
"Social links not allowed"
);
current regex blocks email addresses ending with those domains, but i want to block the urls, e.g:
https://www.facebook.com/username
https://facebook.com/username
http://facebook.com/username

I would just add another regex test using:
/https?\:\/\/(www\.)?facebook\.com\/(.*)/
Which might look something like:
$.validator.addMethod("customurl",
function(value, element) {
var emailTest = /^([\w-.]+#(?!facebook\.com)(?!twitter\.com)(?!instagram\.com)([\w-]+.)+[\w-]{2,4})?$/.test(value);
var urlTest = /https?\:\/\/(www\.)?facebook\.com\/(.*)/.test(value);
return !emailTest && !urlTest;
});

Related

How to validate Text Box to allow Specific URI?

i am developing application using Code-igniter.
I want to validate Text Box Using J Query Or JavaScript That only allowed to input following URL when user submit form.
http://
https://
ftp://
ftps://
file://
market://
linkedin://
fb://
geo:
maps://
Is there any way to do this ?
function is_url(str)
{
regexp = /^(?:(?:https?|ftps?|file?|market?|linkedin?|fb?|maps?):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/;
if (regexp.test(str))
{
return true;
}
else
{
return false;
}
}
Try using this regex in a function that checks validity of your input.
/^(http|https|ftp|ftps|file|market|linkedin|fb|maps|geo):\/\//g
But do the reverse logic:
if it matches then display error mesage and clear input.
If it not matches then your URL is fine
If you wanna prevent URLs anywhere in your input, remove the leading ^ in regex to match even if URL is not start of the string.
like: /(http|https|ftp|ftps|file|market|linkedin|fb|maps|geo):\/\//gm
Here is a crud version of entire solution:
fiddlejs
Main thing is URL validation:
$("#urlInput").focusout(function (){
var inputElement = $("#urlInput");
var regexp = /(http|https|ftp|ftps|file|market|linkedin|fb|maps|geo):\/\//gm;
if (regexp.test(inputElement.val())){
inputElement.val("");
alert("This is nto a valid URL")
}
});
It bound to the
<input id="urlInput" type="url">
You might wanna rework the error reporting in a more user friendly manner but that's another story.

Regex URL validation for angularjs

I'm trying to create validation for URL which will accept the following cases
with HTTP or https or without, with www or without, with subpages or without
http://website.com
https://website.com
http://website.com/
http://www.website.com
website.com
http://website.com/page/id/sdf
I tried the following, but it did not cover all cases above
$scope.urlPattern = '^(([a-z]+[.])?[a-z0-9-]+([.][a-z]{1,4}){1,2}(/.*[?].*)?$'
$scope.urlPattern = '^((https?|ftp)://)?([a-z]+[.])?[a-z0-9-]+([.][a-z]{1,4}){1,2}(/.*[?].*)?$'
I do not have permission to add a comment, So I am editing for my answer only. Below link has all type of URL validation, hope it will help you:
All type URL validation link
If you just want to validate the url, you don't really need to concern the 'www' condition (since it is included in other condition)
Something simple can be done like this:
'^(https?:\/\/)*[a-z0-9-]+(\.[a-z0-9-]+)+(\/[a-z0-9-]+)*\/?$'
JSFiddle:
https://jsfiddle.net/q0d69jq3/2/
var result = url.match(/(http(s)?://.)?(www.)?[-a-zA-Z0-9#:%.+~#=]{2,256}.[a-z]{2,6}\b([-a-zA-Z0-9#:%+.~#?&//=]*)/g);
if (result == null) {
return false;
}
return true;
};

Link/HREF Validation using jQuery or Javascript and RegEx

I have an input form where users can fill out a profile for their internal tool that they are submitting. There is a link field that allows the user to enter a link to this tool.
The following formats are accepted:
http://
https://
file://
\\server\path\to\file.txt
W:\
I am trying create a javascript function to validate the input to make sure it is in one of the above mentioned formats.
I have found some generic URL validation regEx strings but none of them seem to include the local paths of files.
Here is a quick example of a function that works just fine for the top 2 needed formats but when it comes to network paths, I can't find anything that I can get integrated into one easy function.
var message;
var myRegExp =/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/i;
var urlToValidate = "\\server.domain.com\path\file.txt";
if (!myRegExp.test(urlToValidate)){
message = "Not a valid URL.";
}else{
message = "Its a valid URL.";
}
alert(message);
JS Fiddle: https://jsfiddle.net/carlhussey/ebg4m278/
I am looking for a solution to validate the standard web protocols as well as network/file shares.

Forcing a URL field to not have "http://www." using regex

I need to validate a URL field for an input field that deals with a finicky API that will accept URL strings in a very certain format - no www's and no http's attached to the start of the string, or it breaks. A javascript handler grabs the data from the validation.php file. I have everything working except for one field.
So, for example I want
example.com/example
and
example.com
to pass validation, but I want
http://example.com/example
and
http://www.example.com/example
and any variation including any derivation of http:// or www. to not validate.
The regex I have below currently allows any type of URL including http and www. I want it to ONLY allow the above type of URL string, but not sure how to go about this.
// API URL VALIDATION
if ($formfield == "api_url") {
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i", $value)) {
echo "Please enter your URL without the http://www part attached"
} else {
echo "<span>Valid</span>";
}
}
Any ideas?
You can use this simple negative lookahead based regex in preg_match:
~^(?!https?://)(?!www\.).+$~i
RegEx Demo
You can try to "not match" a subject that begins with http or www:
if(!preg_match('/^((http|ftp)s?:\/\/)?(www\.).+?/', $value))
You can do it with .htaccess mod rewrite rule. But in your case it looks like you need to validate a input field so it doesn't apply to all cases. Here is function which will allow/dis-allow such protocols in the url.
function remove_protocol($url) {
$filter_protocol = array('http://', 'https://', 'http://www');
foreach($filter_protocol as $d) {
if(strpos($url, $d) === 0) {
return str_replace($d, '', $url);
}
}
return $url;
}
in the $filter_protocol array you can keep on defining protocols not to allow in urls for instance ftp://

Get a specific string from Url

I know it may sound as a common question, but I have different variables here:
I have an Url like this one:
https://www.facebook.com/events/546604752058417/?suggestsessionid=791e61ca005570613fa552635bc794a5
Now I need to get the number 546604752058417. So logically I should get all what is after events/ but all what is after the first slash after 546604752058417
Now the problem is that the Url may or not start with http, https, www, etc..
I am a little lost and new to Javascript.
What's the simple way to do it?
I have a function that check if the Url is valid and it is from Facebook, but I don't know now how to get that 546604752058417.
Obviously this is only a sample. The function should be able to work with any event id.
function fbProcessSearch() {
var search_url = $("#search_fb_url").val();
if(isFbUrl(search_url)) {
$("#search_fb_event").validationEngine("hide");
// gets the event id from the Url and passes it to the below function.
fbProcess(eid);
}else{
$("#search_fb_event").validationEngine("showPrompt", "Please enter the Facebook event Url", "load", "topLeft", true);
}
}
function isFbUrl(url) {
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*#)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-\/]))?/
return regexp.test(url) && url.indexOf("facebook.com") > -1;
}
Use .match with this regexp: /events\/(.*?)\//
"https://www.facebook.com/events/546604752058417/?suggestsessionid=791e61ca005570613fa552635bc794a5".match(/events\/(.*?)\//)[1]
=> "546604752058417"
Explanation:
events\/ -> Match "/events/" literally
(.*?)\/ -> match anything until first "/" and also capture it
if you know how the url is going to look and can confirm it's facebook. then .split("events/")[1].split("/")[0] to get the code between /events/######/
you can start by "spliting" your url with "/"
var search_url = $("#search_fb_url").val().split("/");
and then you take the field after the one containing "events" ....

Categories