Javascript regular expression for URL validation [duplicate] - javascript

This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 8 years ago.
I want the regular expression to validate the URL with below condition,
It should start with http or https
It should end with the valid domain.
Ex: .com or .in but after the . can have any string. Specially, check whether the . is there and there is a string following the ..
Valid URL: http://www.cnn.com
Invalid URLS:
htt://www.yahoo.com
http://www.yahoo.
http://www.yahoo
I have compose the regular expression as below.
/^(http|https):\/\/[-a-zA-Z0-9+&##/%?=~_|!:,;]+([\-\.]{1}[-a-zA-Z0-9+&##/%?=~_|]+)*\.[a-zA-Z]{2,5}(:[0-9]{1,5})?(\/.*)?$/
It worked fine for most of the scenarios.
But if I enter http://www.yahoo didn't validate correctly, but If I enter http://www.google it throws the validation error.
Can anybody please help me to resolve this issue?

Try this:
^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$

You can use the following reg exp to match the urls
^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
Check it here
https://www.regex101.com/r/jU7iT2/1

Related

email regex not working properly with javascript/angular [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 2 years ago.
I have created a regular expression for email validations.
var regex = "^([a-zA-Z]+([\.-_]?[a-zA-z0-9]+)*)\#([a-zA-Z0-9]+)([-][0-9a-z]+)?\.([a-z-]{2,20})(\.[a-z]{2,3})?$"
To match emails:
1. update#update
2. mohit.bhagat#B-9com.com
3. mohit.Bhagat#us.thalesgroup.com
4. mohit#gmail.com.com.com
If you run this over online website 1 and 4th will fail while 2, 3 will pass.
But when I run this code in Javascript( Browser console ), 1st also passes the validation.
I am using Angular application.
The problem is how JS ignores \.
If you do following
var regex = "[a-z]+#[a-z]+\.[a-z]{2,3}"
Resultant string stored is
"[a-z]+#[a-z]+.[a-z]{2,3}"
And if you do this
var regex ="[a-z]+#[a-z]+[\.][a-z]{2,3}"
Resultant string stored is
"[a-z]+#[a-z]+[.][a-z]{2,3}"
Pay attention to [.] with this now i was able to get validation error for 1st email.
Complete regex: "^([a-zA-Z]+([-_.]?[a-zA-Z0-9])*)#([a-zA-Z0-9]+([-][a-z0-9A-Z]+)?)[.]([a-z]+[-_]?[a-z]+)([.][a-z]{2,3})?$"
Update:
Or you can do this: var regex ="[a-z]+#[a-z]+\\.[a-z]{2,3}"
As mentioned in comments below, you can do this way to consider . by including it in [.], but its a hack and not origional way to do it.
Best way to have . included in your regex is \.

Javascript Regex not working while used in a angular controller [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Match exact string
(3 answers)
Closed 5 years ago.
I have written the following function in javascript to validate a string that I will use as a file name. I would like to check for all the characters that are restricted by Windows OS as invalid while creating files. I checked the regular expression at RegExr, it seems to be working as expected but it doesn't work when called from an Angular controller and it only matches the first character in the parameter. I'm adding the file extension later on so that isn't a problem.
Can anybody help with it? I'm relatively new to regular expressions and would appreciate any help or pointers to useful resources.
function validateInput(value) {
if (!AngularUtils.isUndefinedOrNull(value)) {
var regex = new RegExp("[^<>/\\\\=|:*?\"]+$");
var regexOutput = regex.test(value);
if (!regex.test(value))
return true;
}
return false;
}
Edit:
Even after changing the regex to handle javascript constructors, I'm still getting valid matches for the following input: "sample_css", "sample=css","=sample"
Only the first string should be valid. jsfiddle here.

How to check string in current URL with javascript? [duplicate]

This question already has answers here:
Get current URL with jQuery?
(33 answers)
Closed 8 years ago.
My Url looks like:
http://mywebsite/series/2545-abc
But I don't know how to check URL with regex. So, How to check url current windows with above URL?
I mean:
If Url = http://mywebsite/series/2545-abc
do something
What about this? The regular expression defined at the first line is tested against the URL:
var myRe = /\/series\//; //regular expression to use
if(myRe.test(window.location.href)){
alert("matching");
}else{
alert("not matching");
}
If you want to test for the whole URL (and really want to use a regular expression for that), you could replace the first line with
var myRe = /^http:\/\/mywebsite\/series\/2545-abc$/;
If you remove the dollar sign at the end, modifications at the end of the URL are accepted as well (e.g. http://mywebsite/series/2545-abc/foo.html)

javascript url regexp restrict multiple http(s)/www [duplicate]

This question already has answers here:
Regular expression for URL
(10 answers)
Closed 8 years ago.
here is my regexp
var url_reg = /^(http[s]?:\/\/|ftp:\/\/)?(www\.)?[a-zA-Z0-9-\.]+\.(com|org|net|mil|edu|ca|in|au)+/;
it works fine for single input like https://www.google.com , but
it allows double or more "http/https/www" like below -
https://www.google.com/https://www.google.com/
url can also include folder like google.com/folder/file
i need to validate single occurrence of valid url.
Can anyone help me?
To validate a URL, you can use a regex. This is what I use. A valid URL per the URL spec. The URL you have provided, is actually a valid URL per the URL spec.
/^((((https?|ftps?|gopher|telnet|nntp):\/\/)|(mailto:|news:))(%[0-9A-Fa-f]{2}|[-()_.!~*';\/?:#&=+$,A-Za-z0-9])+)([).!';/?:,][[:blank:]])?$/
This was borrowed from OSWAP

Regular expression for URL validation in javascript [duplicate]

This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 9 years ago.
I am having form which takes url as input..I want to validate it using javascript and regular expression..
it should accept
www.google.com
google.com
http://google.com
Please suggest me a regular expression and total code if possible for validation of url...
i tried lotta url given on many forums..but couldn't get appropriate answer..
thanks in advance
try this,
^(http\://)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$
this will support
http://google.com
http://www.google.com
www.google.com
google.com
You can test it easily with jQuery :
http://docs.jquery.com/Plugins/Validation/Methods/url.
try dis
url_reg = /(ftp|http|https):\/\/(\w+:{0,1}\w*#)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-\/]))?/;
This one would be fine, (http|https|)(://|)([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
Try it here. works fine - http://www.regular-expressions.info/javascriptexample.html

Categories

Resources