shortening form validation jQuery code -
i have written code, , works find, feel i'm being stupid here , shorten code. there anyway shorten this? please leave answer, thank you.
$('.submit-form').on('click' , function() { if($('.form-signup1').val().length === 0) { $('.form-signup1').addclass('error') } if($('.form-signup2').val().length === 0) { $('.form-signup2').addclass('error') } if($('.form-signup3').val().length === 0) { $('.form-signup3').addclass('error') } if($('.form-signup4').val().length === 0) { $('.form-signup4').addclass('error') } if($('.form-signup5').val().length === 0) { $('.form-signup5').addclass('error') } if($('.form-signup6').val().length === 0) { $('.form-signup6').addclass('error') } });
edit: thank answers, appreciate time. chose 1 understand best. again!
add common class elements then
$('.submit-form').on('click', function () { $('.form-signup').each(function () { var $this = $(this); if ($this.val().length === 0) { $this.addclass('error'); } }) });
Comments
Post a Comment