egoselfaxis
asked on
Need to update URL validation javascript function to require http:// or https:// prefix
I need to update the following URL validation javascript so that it checks to make sure that the entered url is prefixed with either http:// or https://
function isValidURL(url){
var RegExp = /^(([\w]+:)?\/\/)?(([\d\w] |%[a-fA-f\ d]{2,2})+( :([\d\w]|% [a-fA-f\d] {2,2})+)?@ )?([\d\w][ -\d\w]{0,2 53}[\d\w]\ .)+[\w]{2, 4}(:[\d]+) ?(\/([-+_~ .\d\w]|%[a -fA-f\d]{2 ,2})*)*(\? (&?([-+_~. \d\w]|%[a- fA-f\d]{2, 2})=?)*)?( #([-+_~.\d \w]|%[a-fA -f\d]{2,2} )*)?$/;
if(RegExp.test(url)){
return true;
}else{
return false;
}
}
Please advise.
Thanks!
- Yvan
function isValidURL(url){
var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]
if(RegExp.test(url)){
return true;
}else{
return false;
}
}
Please advise.
Thanks!
- Yvan
change:
^(([\w]+:)?\/\/)?
to:
^((http:)?\/\/)?
Also, at the end of the RegEx, you need to add the "i" to make it case insensitive:
...?$/i;
^(([\w]+:)?\/\/)?
to:
^((http:)?\/\/)?
Also, at the end of the RegEx, you need to add the "i" to make it case insensitive:
...?$/i;
Edited version:
function isValidURL(url){
var RegExp = /^((https?:)\/\/)?(([\d\w] |%[a-fA-f\ d]{2,2})+( :([\d\w]|% [a-fA-f\d] {2,2})+)?@ )?([\d\w][ -\d\w]{0,2 53}[\d\w]\ .)+[\w]{2, 4}(:[\d]+) ?(\/([-+_~ .\d\w]|%[a -fA-f\d]{2 ,2})*)*(\? (&?([-+_~. \d\w]|%[a- fA-f\d]{2, 2})=?)*)?( #([-+_~.\d \w]|%[a-fA -f\d]{2,2} )*)?$/i;
if(RegExp.test(url)){
return true;
}else{
return false;
}
}
function isValidURL(url){
var RegExp = /^((https?:)\/\/)?(([\d\w]
if(RegExp.test(url)){
return true;
}else{
return false;
}
}
ASKER
Heilo -- does your edited version only check for https:// prefixes? It seems it based on what I'm seeing in the code.
- yg
- yg
Hi,
I've just checked :
^(http|https)://(([\w]+:)? \/\/)?(([\ d\w]|%[a-f A-f\d]{2,2 })+(:([\d\ w]|%[a-fA- f\d]{2,2}) +)?@)?([\d \w][-\d\w] {0,253}[\d \w]\.)+[\w ]{2,4}(:[\ d]+)?(\/([ -+_~.\d\w] |%[a-fA-f\ d]{2,2})*) *(\?(&?([- +_~.\d\w]| %[a-fA-f\d ]{2,2})=?) *)?(#([-+_ ~.\d\w]|%[ a-fA-f\d]{ 2,2})*)?$
out here
http://www.regular-expressions.info/javascriptexample.html
and it works correctly?
so :
http://www.test.com : successful
https://www.test.com : successful
www.test.com : no match
I've just checked :
^(http|https)://(([\w]+:)?
out here
http://www.regular-expressions.info/javascriptexample.html
and it works correctly?
so :
http://www.test.com : successful
https://www.test.com : successful
www.test.com : no match
>>does your edited version only check for https:// prefixes? I
No. It takes into consideration, both http and https
This:
https?
means that the "s" is optional. In other words, it could start with:
http OR https
No. It takes into consideration, both http and https
This:
https?
means that the "s" is optional. In other words, it could start with:
http OR https
ASKER
I tested it and it doesn't work.
>>I've just checked :
>>and it works correctly?
try:
http://www://google.com
OR
HTTP://www.google.com
>>and it works correctly?
try:
http://www://google.com
OR
HTTP://www.google.com
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I ended up doing the url validation on the server-side (by adding the http:// prefix whenever necessary) -- but thanks for your help.
- Yvan
- Yvan
var RegExp = /^(http|https)://(([\w]+:)