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

Question

Question

Form table date must not be earlier than Submission date

asked on May 18, 2020 Show version history

Dear All,

I have an Event form in which the submitter adds Event Dates and times in a table.  The Event dates must always be later than the Submission date.

I need a warning to be issued if the Event date is too early, and I have tried to achieve this in jscript.  The following code is wrong as the IF statement is looking at the day and not the whole date.  So in this example my code would give a warning for 16/06/2020.

I have tried to use Date.parse but my jscript experience is limited.  Any help would be appreciated.

 

//where  .eventDate is the Event Date column and .SubmitDate is the Submission Date
$(document).ready(function () {
  $('.cf-table-block').on('change','.eventDate input',function() {

     $('.cf-table-block tr').each(function() {
       var eventdate = $(this).find('.eventDate input').val(); 	
       var subdate   = $('.SubmitDate input').val();

       if(eventdate <= subdate){
         
       alert('The eventDate must be later than the SubmitDate');
           }
     });
  }); 
});

 

0 0

Answer

SELECTED ANSWER
replied on June 2, 2020

I was adding to your JavaScript with the assumption it was targeting correctly, and did not test this in a form myself.

To use it in a table, you can do it two ways:

  1. On change of one, you can parse the row from the field's id (#Field4(2) --> q4, row 2 in the table) and then target the other date field in the same row by id.
  2. On change of any, update all of them. In this case, you could target all of them with the classes like you have, and then run through them as arrays, both at the same time. So for example, eventDate[i] and subDate[i] would be in the same table row.
1 0

Replies

replied on May 19, 2020

Hi Peter,

The JavaScript you have is only comparing the strings / the text of the date, so (for example) "22/06/2022" will be 'earlier' than "30/02/2004". If you change the date fields' formatting to be yyyy-MM-dd then this will always be accurate and you don't need to change the JS at all.

2 0
replied on May 21, 2020

Thanks Jim,

An easy solution but unfortunately my customer will not accept yyy-MM-dd.

0 0
replied on May 22, 2020

Then before this line:

if(eventdate <= subdate){

you can change the strings to a comparable format before comparing (yyyyMMdd).

eventdate = eventdate.substr(6,4) + eventdate.substr(3,2) + eventdate.substr(0,2);
subdate = subdate.substr(6,4) + subdate.substr(3,2) + subdate.substr(0,2);

 

0 0
replied on June 1, 2020

Thanks Jim, I can get this to work for separate date fields, but I cannot get it to work for a date field in a table (as in above image where I am comparing the submit date to a date in the table).

Have you tried this in a table?

0 0
SELECTED ANSWER
replied on June 2, 2020

I was adding to your JavaScript with the assumption it was targeting correctly, and did not test this in a form myself.

To use it in a table, you can do it two ways:

  1. On change of one, you can parse the row from the field's id (#Field4(2) --> q4, row 2 in the table) and then target the other date field in the same row by id.
  2. On change of any, update all of them. In this case, you could target all of them with the classes like you have, and then run through them as arrays, both at the same time. So for example, eventDate[i] and subDate[i] would be in the same table row.
1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.