Get a specific string from Url - javascript

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" ....

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.

Retrieve url query string using javascript to resolve a if/else statement and redirect

I've read about 6 other posts on here about this topic and I still can’t seem to get this to work.
I want to use pure javascript to read a user defined url query string, then have it make a decision about where to redirect the user based on the information in the string.
I have the javascript saved in “script.js”,
this is part of a webpage “https://www.website.com/script.js”,
the url with the query string would look like this “https://www.website.com/script.js?bird=chicken”.
This is what my code looks like:
}
var birdtype = getQueryString('bird');
if ( birdtype == [ chicken ])
window.location.replace = "https://www.website.com/chicken.html";
else
window.location.replace = "https://www.website.com/turkey.html";
};
Please, what am I doing wrong?
You can use .href property instead of replace like
window.location.href = "https://www.website.com/chicken.html"
to redirect to a different page.

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;
};

Regex to block domains in url field

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;
});

Javascript JSON display hashtag symbol

I'm saving some data through the URL and one of these data elements is a text. I want to be able to save and store notes with hashtags on them. For example, I want to be able to save "I believe in #yolo", but when I save it (through php), and retrieve it again with javascript, I get "I believe in". Anyway, I can get that #yolo back? I tried using str_replace, but I may have gotten the regex wrong.
Thanks for all the help!
function dropsuccess() {
answerArray = [];
answerArray.id = Math.round(new Date().getTime() / 1000);
answerArray.text = document.getElementById("boglory").value;
$.getJSON("saveBlade.php?id="+answerArray.id+"&text="+answerArray.text,
function(data) {
if (data.hasOwnProperty("error")) {
alert(data.error);
} else {
$("#handle").animate({marginTop:"100%"},800,
function() {
document.getElementById("handle").style.display = "none";document.getElementById("bladecontainer").style.display = "none";$('#qod').fadeIn('medium');
});
}
});
}
answerArray.text includes the text that is being passed. Ex ("I believe in #yolo.") #yolo is a string, not a div-id tag.
The problem is that # is a reserved character in URLs to indicate a jump to an anchor.
You need to escape it. JQuery automaticly escape all characters that need to if you supply them as the data argument in the getJSON function:
$.getJSON("saveBlade.php",
{
id: answerArray.id,
text: answerArray.text
},
function(data) {
Look here for details

Categories