Link to home
Start Free TrialLog in
Avatar of egoselfaxis
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,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})*)?$/;
    if(RegExp.test(url)){
        return true;
    }else{
        return false;
    }
}

Please advise.

Thanks!
- Yvan

Avatar of darren-w-
darren-w-
Flag of United Kingdom of Great Britain and Northern Ireland image

Try this:

   var RegExp = /^(http|https)://(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-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})*)?$/;
Avatar of hielo
change:
^(([\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,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})*)?$/i;
    if(RegExp.test(url)){
        return true;
    }else{
        return false;
    }
}

Avatar of egoselfaxis
egoselfaxis

ASKER

Heilo -- does your edited version only check for https:// prefixes? It seems it based on what I'm seeing in the code.  

- yg
Hi,

I've just checked :

^(http|https)://(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-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
>>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
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
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I ended up doing the url validation on the server-side (by adding the http:// prefix whenever necessary) -- but thanks for your help.

- Yvan