jQuery script to automatically add HTTP:// to a field

The following jQuery script can be used to automatically add HTTP:// to the start of a input field value if the value doesnt already start with either HTTP:// or HTTPS://

It has been written for WordPress using the Gravity Forms plugin – so uses ‘jQuery’ instead of the native $.

If you’re using jQuery natively, change jQuery to $

The script will:

  • Load when the page is ready
  • Target an input field (#field_28_126 input)
  • When the field is clicked out of (blur) – checks if the field does not start with HTTP
  • If it does not start with HTTP, HTTP:// is added to the value automatically.

To install the script, simply add the script to your page (you may need to add jQuery if you havent already) and change the targeted field (#field_28_126 input)

jQuery(function() {
jQuery('#field_28_126 input').blur(function(e) {
    var ini = jQuery(this).val().substring(0, 4);
    if (ini !== '' && ini !== 'http'){
        // get value from field
        var cur_val = jQuery(this).val(); 
        // do with cur_val
        jQuery(this).val('http://' + cur_val);
    }        
});
});