creating a JQuery function
Posted
by
Russell Parrott
on Stack Overflow
See other posts from Stack Overflow
or by Russell Parrott
Published on 2010-12-24T08:36:00Z
Indexed on
2010/12/24
8:54 UTC
Read the original article
Hit count: 280
Sorry to bother you guys & girls again on Christmas eve, but I need help creating a reusable JQuery function. I have "badly crafted" this set of code that all works. But I would really like to put it as a function so I don't have to keep repeating everything for each form. I am not too sure about how all the if statements can be combined etc. that is why I wrote it as it is. Any help much appreciated - Oh I suppose it could also be some kind of plugin but that might be the next step if I can understand how the function works.
$(':input:visible').live('blur',function(){
if($(this).attr('required')) {
if($(this).val() == '' ) {
$(this).css({'background-color':'#FFEEEE' });
$(this).parent('form').children('input[type=submit]').hide();
$(this).next('.errormsg').html('OOPs ... '+$(this).prev('label').html()+' is required');
$(this).focus();
$(this).attr('placeholder').hide(); }
else {
$(this).css({'background-color':'#FFF' , 'border-color':'#999999'});
$(this).next('.errormsg').empty();
$(this).parent('form').children('input[type=submit]').show(); }
}
return false;
});
$(':input[max]').live('blur',function(){
if($(this).attr('max') < parseInt($(this).val()) ){
$(this).next('.errormsg').html( 'OOPs ... the maximum value is '+$(this).attr('max') );
$(this).parent('form').children('input[type=submit]').hide();
$(this).focus();
} else {}
return false;
});
$(':input[min]').live('blur',function(){
if($(this).attr('min') > parseInt($(this).val()) ){
$(this).next('.errormsg').html( 'OOPs ... the minimum value is '+$(this).attr('min') );
$(this).parent('form').children('input[type=submit]').hide();
$(this).focus();
} else {}
return false;
});
$(':input[maxlength]').live('keyup',function(){
if($(this).val()==''){ }
else { $(this).next('.errormsg').html( $(this).attr('maxlength')- $(this).val().length +' chars remaining'); }
return false;
});
As said, help much appreciated with one small (I hope) thing, how can I break out of any function IF there are no error messages to actually submit the form?
© Stack Overflow or respective owner