How to check URL containing http using jQuery and RegEx

How can I check and add http (if it does not exist) in the given url using jQuery and RegEx?

I tried the following:

jQuery("#text_box_url").blur(function() { if (jQuery(this).val()) { if(jQuery(this).val().match(/~^(?:f|ht)tps?:/)) jQuery(this).val("http://"+jQuery(this).val()); } }); 

Thanks in advance.

+4
source share
1 answer

A working example is here: http://jsfiddle.net/jkeyes/dYbfY/2/

 $("#text_box_url").blur(function() { var input = $(this); var val = input.val(); if (val && !val.match(/^http([s]?):\/\/.*/)) { input.val('http://' + val); } }); 

Update the solution that leaves all the values ​​with the scheme intact: http://jsfiddle.net/jkeyes/c6akr9y2/

+13
source

All Articles