Force HTTP:// prefix in url form inputs w jQuery

Here's a quick n' dirty way to make sure any URL in a form input starts with "http://"



    $('form input.url').keyup(function(){
        if (
            ($(this).val().length >
6) && ($(this).val().substr(0,7) != 'http://')
            || ($(this).val() == '')
            ){
            $(this).val('http://' + $(this).val());    
        }
    });

This ensures that the contents of the input will start with http://. The length > 6 is a bit of a hack, but if you delete everything in the field, or start typing over 6 chars (i.e. long enough to look for http:// at the beginning of the string), the prefix gets inserted for you. I am also loading my input with value="http://", but this could also be done w/ the function above by triggering it on page load rather than just on keyup for the specified input.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
blogcfc 5.9.1.002 by raymond camden
contact michael evangelista