AnsweredAssumed Answered

form submission error after validating

Question asked by vinayak Tankasali on Mar 12, 2015
I have module called accounts where  I Have 5 address fields I have made validation by creating a validate.js file in accounts/module/validate.js and giving this path in editviewdefs.php in custom/accounts/metadata/editviewdefs.php all validation is working fine.
My problem if any one textfield is filled and other 4 left empty.It must not submit the form after clicking Save Button and Infact I have tried out in Many ways i.e returning false or by adding event.default in my jquery function but it is not working can any one help me out to stop this form submission if text field left empty. my code is as below.....



$('#SAVE_HEADER').click(function() {    
         if ($('#billing_address_street').val()!='' && $('#billing_address_city').val()!='' && $('#billing_address_state').val()!='' && $('#billing_address_postalcode').val()!='' && $('#billing_address_country').val()!='') 
      {
         $("span#error_msg").html("");  
        
    }       
          else {        
      var dataInForm = false;
      var allFieldsFilled = true; 
      var requiredFields = ['billing_address_street', 'billing_address_city', 'billing_address_state', 'billing_address_postalcode', 'billing_address_country'];
      
      // Know if there are data in form
      for (var i = 0; i < requiredFields.length; i++) {
          var val = $('#' + requiredFields[i]);
          if (val.val() != '') {
              // there are data in form so put true
              dataInForm = true;
                
          }

      }

      // If there is data in form, loop tu search empty fields
      if (dataInForm) {
          for (var ii = 0; ii < requiredFields.length; ii++) {
              var val2 = $('#' + requiredFields[ii]);
              if (val2.val() !== '') {
                  allFieldsFilled = false;
              }
          }
 
      }

      // put message if there is at least 1 data in form && fields are missing
     if (dataInForm && !allFieldsFilled) {
       if (!$( "#error_msg" ).length ) { 
       $("<span id='error_msg'></span>").appendTo('#billing_address_fieldset');
       $("span#error_msg").html("Enter all the Address Fields!");
     
       }
         
 
      } 
     

          
                }
         

 
     
}); 

Outcomes