You are viewing limited content. For full access, please sign in.

Question

Question

Make table required, but not specific fields

asked on October 15, 2019

I have a table with 5 rows that requires data in fields in any one row. Is there a way to make the table required instead of the field?

1 0

Answer

SELECTED ANSWER
replied on October 17, 2019

Hi Kathy,

I see, you can certainly achieve this functionality with some custom JS. If you add the class "myTable" to your table and add a custom HTML field with an error message such as "Table Requires Data" with the css classes "tablerrormessage hidden" , the below code added to your Custom Javascript should hide and show an error message when the table contains no data, and validation will be done on table change and when submit is clicked



// A $( document ).ready() block.
$(document).ready(function(){ 
    
  
  $(".myTable table tbody").on("change","input",function(){
    checkTable() == true ? $(".tableerrormessage").hide() : $(".tableerrormessage").show() 
    //if table is changed hide or show error depending if data was found
    
  });
  
  $(".Submit").on("click",function(){
   return checkTable()
   // if checkTable returns true, form submits if no other errors, or if false form will not submit
  });

  
}); // end of document ready


function checkTable(){ // checkTable Function (returns if true if table contains any data)
  
     var tableRowsWithData = 0  
    
    $(".myTable table tbody input").each(function(){ //loop over table inputs
      
     if ($(this).val()) tableRowsWithData += 1 // if data found add 1 to count
      
    });
    
  if (tableRowsWithData) return true; // if data found return true
    
  else {

      $(".tableerrormessage").show() 
      return false;
      }   // no data found show error and return false to prevent submit
  
}

2 0

Replies

replied on October 16, 2019

There is no out of the box way to simply make a table required. But checking the required tick box on each column in a table will have the same effect and in form a user that the field they just left empty is required. 

You could use some custom Jquery to scan all the table inputs on submit to check if all of its contents are filled and throw an error if not.  But i see no real need if you can simply use the required fields to prevent submission.

0 0
replied on October 16, 2019

Thank you, Christian. Unfortunately when you choose required on the field it wants a value in each row of the table. I need some data in the columns, but not in all rows. The example below illustrates how I want to be able to validate that the table has data, but not each row. 


1 0
SELECTED ANSWER
replied on October 17, 2019

Hi Kathy,

I see, you can certainly achieve this functionality with some custom JS. If you add the class "myTable" to your table and add a custom HTML field with an error message such as "Table Requires Data" with the css classes "tablerrormessage hidden" , the below code added to your Custom Javascript should hide and show an error message when the table contains no data, and validation will be done on table change and when submit is clicked



// A $( document ).ready() block.
$(document).ready(function(){ 
    
  
  $(".myTable table tbody").on("change","input",function(){
    checkTable() == true ? $(".tableerrormessage").hide() : $(".tableerrormessage").show() 
    //if table is changed hide or show error depending if data was found
    
  });
  
  $(".Submit").on("click",function(){
   return checkTable()
   // if checkTable returns true, form submits if no other errors, or if false form will not submit
  });

  
}); // end of document ready


function checkTable(){ // checkTable Function (returns if true if table contains any data)
  
     var tableRowsWithData = 0  
    
    $(".myTable table tbody input").each(function(){ //loop over table inputs
      
     if ($(this).val()) tableRowsWithData += 1 // if data found add 1 to count
      
    });
    
  if (tableRowsWithData) return true; // if data found return true
    
  else {

      $(".tableerrormessage").show() 
      return false;
      }   // no data found show error and return false to prevent submit
  
}

2 0
You are not allowed to follow up in this post.

Sign in to reply to this post.