Why in following code don’t work this part if(!$(this).val()){... from code and this match is true with http://www.st, i want as: http://stackoverflow.com ?
Example: http://jsfiddle.net/gp9nL/3/
$('.url').keyup(function(){
if (!$(this).val().match(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \?=.-]*)*\/?$/)){
$(this).css("background", "#ffc4c4");
result = false;
}else {
$(this).css("background", "#FFFFEC");
result = true;
}
if(!$(this).val()){
alert('field is empty')
$(this).css("background", "#FFFFEC");
}
return result;
});
This
if(!$(this).val())is probably not working because it is being called on
keyup$('.url').keyup(function(){Therefore, there will always be something in the
input, as a key stroke is needed to fire keyup and it will never evaluate as empty.EDIT
As per the comment
If you want to check if the field is empty, do the check on
focusout.Here is your updated code:
And your fiddle updated: http://jsfiddle.net/gp9nL/4/