Validate a URL with jQuery and add http:// and final / if needed
<p>I am doing a simple URL Builder in a backend of a website where I need a url like <a href="http://www.google.es/" rel="nofollow">http://www.google.es/</a> where <strong>http://</strong> and <strong>/</strong> are needed but the users usually inputs www.google.es or maxmium <a href="http://www.google.es" rel="nofollow">http://www.google.es</a>.</p>

<p>I have this basic code where I check for not blank values and for a <a href="http://stackoverflow.com/questions/2723140/validating-url-with-jquery-without-the-validate-plugin">valid url</a>:</p>

<pre><code>$("#ga-generate").click(function(){
    var url = $("#ga-url").val();
    var nombre = $("#ga-nombre").val();

    if(url == "") {
        alert("URL is required");

        return false;
    }
    else {
        //set the new URL with http and / if needed

        $("#ga-url").val(url);
    }

    if(nombre == "") {
        alert("A name is required");

        return false;
    }

    if(/^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:)*@)?((\[(|(v[\da-f]{1,}\.(([a-z]|\d|-|\.|_|~)|[!\$&amp;'\(\)\*\+,;=]|:)+))\])|((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=])*)(:\d*)?)(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)*)*|(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)*)*)?)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)*)*)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)){0})(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(url)) {
        var final_url = "";

        final_url = url;    
    }
    else {
        alert("URL is not valid");
    }

});
</code></pre>

<p>The regex checks for a valid URL, it checks if it have <em>http://</em> (and not a final / ) so I would like to add the http:// and / if it's not added just before call the regex. How I can do it?</p>

<p>Thank you in advance!!</p>

<p><strong>EDIT</strong>: for the first part, add http, there is a PHP function that I don't know how to translate to javascript:</p>

<pre><code>function addhttp($url) {
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
        $url = "http://" . $url;
    }
    return $url;
}
</code></pre>
