Skip to content Skip to sidebar Skip to footer

Form That Removes Www. And Prints Result On Input?

I need to make a form similar to the one 'shorten link' sites use. It should simply remove WWW. and echo the result so I later add my code around it. For example if the user types

Solution 1:

You can do lots of fancy stuff with regular expressions. For example, this javascript will do what you want:

// Event for enter click
$("#url").keypress(
    function(e) {
        if(e.keyCode == 13) {
            $("#output").html(cleanURL($("#url").val()));
        }
    }
);

// Event for button click
$("#submit").click(
    function() {
        $("#output").html(cleanURL($("#url").val()));
    }
);

// Function to clean urlfunctioncleanURL(url)
{
    if(url.match(/http:\/\//))
    {
        url = url.substring(7);
    }
    if(url.match(/^www\./))
    {
        url = url.substring(4);
    }

    return url;
}

Works on enter click, button click and removes both http:// and www

You can try it out here: http://jsfiddle.net/Codemonkey/ydwAb/1/

Post a Comment for "Form That Removes Www. And Prints Result On Input?"