0
         $('#formRegisterUser').submit(function(){
      
            let inpUsername = $('input[name="username"]').val();
            let inpEmail = $('input[name="email"]').val();
            let inpPassword = $('input[name="password"]').val();
            let inpConfirmPassword = $('input[name="confirmPassword"]').val();
    
            if(inpUsername == '' || inpEmail == '' || inputPassword == '' || inpConfirmPassword == ''){
              return false;
            } else {
              return true;
            }
         }

I want to check whether if any of form input is empty return false else return true.But this way is kinda repititive.Is there any easy way? with just less code to do the same code with Jquery.Thanks

Mishen Thakshana
  • 143
  • 1
  • 12

1 Answers1

0

Just make a jquery prototype function, same syntax below.

jQuery.prototype.isEmpty = function(){
    return this.context.value == '';
}

let isEmpty = $('input[name="username"]').isEmpty();

Samm
  • 16
  • $("#yourform input").each(function(){ if ($.trim($(this).val()).length == 0){ $(this).addClass("highlight"); isFormValid = false; //$(this).focus(); } – JulesUK Apr 13 '22 at 15:11