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;
};
Related
Is this possible to always redirect
from
www.name.blogspot.com/title
to
www.name.blogspot.com/p/title.html
For example, when someone type www.name.blogspot.com/title
so it is automatically redirect to www.name.blogspot.com/p/title.html
maybe need some javascript that always auto-generate to convert incoming url to the correct url.
usually, I used this script below, but it still manually method, so edited one by one if there is many links.
<script>
if(window.location.href == 'https://www.name.blosgpot.com/title')
{
window.location="https://www.name.blosgpot.com/p/title.html";
}
</script>
Thankyou for your help
Idealy you want to do this in the server, for example, if you are using Nginx, you can rewrite incomming requests with a given pattern URL to another path:
server{
...
location /title {
rewrite ^/(.*)$ https://www.newsite.com/p/$1 redirect;
}
...
}
But if you need to do it on the client, you can use a similar logic, matching patterns and "rewriting" the URL, something like this:
const myPattern = /https:\/\/name.blosgpot.com(.*)/
const path = window.location.href.match(myPattern)[1]
if (path) {
window.location.href = "https://www.name.blosgpot.com/p" + path
}
I was wondering if there is a programmatic way of sanitizing (user provided) URLs in AngularJs 1.5. I know there is $sanitize service, which is for sanitizing HTML but I can't find something that would work for URL.
Sample Good URL:
http://www.somedomain.com
Sample Dangerous URL:
javascript:document.write('Time for some fun');
There is undocumented $$sanitizeUri service that does this. It's basically a function that adds unsafe prefix to supplied URL. There's no need to use it for URLs are bound to href like
<a href="{{url}}">
because this is already done by compiler ($$sanitizeUri is used there).
For malformed URLs in general it may be better to process them by hand and possibly add http:// if it doesn't contain acceptable (http/https) protocol.
I ended up performing the following regex test (inspired from $$sanitizeUri service) on the URLs and then performing a special operation whenever the check failed.
function navigateToUrl(url) {
var safeWhiteListRegex = \^(https?|ftps?|file?):\/\/\gi;
if (!safeWhiteListRegex.test(url)) {
// url = ...;
} else {
$window.open(url, "_blank");
}
}
Along the way, one of my friends also discovered the #braintree/sanitize-url Node Module, which I also think is something people can use for situations like this.
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.
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" ....
I know this question has been posted many times. But still i would like to inquire a little more. I have used a function
function validateEmail(elementValue){
var emailPattern = /^[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,3}$/;
return emailPattern.test(elementValue);
}
It works fine. But my problem is that it allows 123#123.com as a valid email. So i want to check the domain also like checkdnsrr() does in php. Can i do it in javascript? I want to check for valid domain also.
It looks like there is no other solution than using AJAX to do this.
You do a request on your server with JS, the server checks the DNS using checkdnsrr(), and it responds with whatever you want, it will allow your JS to handle the validation depending on this.
Why don't you do it in php? That will be much easier than making an ajax call.
On the same code that you have Provided
function validateEmail(elementValue){
var emailPattern = /^[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,3}$/;
return emailPattern.test(elementValue);
}
Change it in this manner and it will validate 123#123.com Checking that the Domain can not be #123.com
function validateEmail(elementValue){
var emailPattern = /^[a-zA-Z0-9._-]+#[a-zA-Z-]+\.[a-zA-Z]{2,3}$/;
return emailPattern.test(elementValue);
}