Need to update URL validation javascript function to require http:// or https:// prefix

egoselfaxis
egoselfaxis used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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})*)?$/;
Expert of the Year 2008
Top Expert 2008

Commented:
change:
^(([\w]+:)?\/\/)?

to:
^((http:)?\/\/)?

Also, at the end of the RegEx, you need to add the "i" to make it case insensitive:
...?$/i;
Expert of the Year 2008
Top Expert 2008

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

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
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
Expert of the Year 2008
Top Expert 2008

Commented:
>>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

Author

Commented:
I tested it and it doesn't work.
Expert of the Year 2008
Top Expert 2008

Commented:
>>I've just checked :
>>and it works correctly?
try:

http://www://google.com

OR
HTTP://www.google.com
Expert of the Year 2008
Top Expert 2008
Commented:
copy and paste the function below
<script type="text/javascript">
function isValidURL(url){
    var Regx = /^((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(Regx.test(url)){
        return true;
    }else{
        return false;
    }
}

alert( isValidURL("http://www.google.com") )
alert( isValidURL("https://www.google.com") )
alert( isValidURL("http://www://google.com") )
alert( isValidURL("www.google.com") )

</script>

Open in new window

Author

Commented:
I ended up doing the url validation on the server-side (by adding the http:// prefix whenever necessary) -- but thanks for your help.

- Yvan

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial