Jump to content

JS Date Restrictions Quit Working


Recommended Posts

Years ago I added some JavaScript to my forms to prevent clients from selecting a "due date" that isn't at least two weeks in the future. Recently that code has stopped working, and all submissions are allowed through, regardless of the date selected. I think this may be tied to the recent Caspio updates. At least, that's all that I can see; the forms themselves haven't changed.

Below I'm pasting my JS, which resides in the footer area of my forms. Any ideas would be greatly appreciated. Thanks! (BTW, I know two weeks is 14 days, not 13. But my users don't seem to recognize that, so I made this small allowance.)
 

<SCRIPT LANGUAGE="JavaScript">

function check_date()
{
var entered_value = document.getElementById("InsertRecorddateNeeded").value;
var entered_date = new Date(entered_value);

var two_week_date = new Date();
two_week_date.setDate(two_week_date.getDate() + 13);

if(entered_date < two_week_date)
   {
      alert("Must be at least 2 weeks in future");
      return false;
 }
}

document.getElementById("caspioform").onsubmit=check_date;
</SCRIPT>

 

Link to comment
Share on other sites

@abitaquest,

There's a possibility that the reason it's not working now is because of the recent Caspio update. Event like onsubmit will no longer be supported and you need to use Caspio's built-in event handlers. Kindly please check the link below for more information:

https://howto.caspio.com/release-notes/caspio-bridge-13-0/13-0-impacted-areas/

https://howto.caspio.com/datapages/ajax-loading/

I hope this helps.

Link to comment
Share on other sites

  • 2 weeks later...

Hi, edujobs2k10 —

This is untested, but it should work. I only changed the variable name and set "permitted_date" to be today's date plus 0 days, then changed the text of the error message. The user should now only be allowed to select today or a day in the future, nothing in the past.

First, be sure that AJAX isn't allowed to run on your form. Then, paste the following into your form's footer:

<SCRIPT LANGUAGE="JavaScript">

function check_date()
{
var entered_value = document.getElementById("InsertRecorddateNeeded").value;
var entered_date = new Date(entered_value);

var permitted_date = new Date();
permitted_date.setDate(permitted_date.getDate() + 0);

if(entered_date < permitted_date)
   {
      alert("Cannot be a date in the past");
      return false;
 }
}

document.getElementById("caspioform").onsubmit=check_date;
</SCRIPT>
Link to comment
Share on other sites

On 9/3/2018 at 9:47 AM, edujobs2k18 said:

Hi Guys,

How to block user to choose past date from calendar in a form? User should not allow to choose a past date from calendar in "Date0" field. I am unable to do so.

Hi @edujobs2k18,

You may try to use the following Javascript solution:  

<script type="text/javascript">
document.addEventListener('BeforeFormSubmit', function(event) {
var entered_value = document.getElementById("InsertRecordDate0").value;
var entered_date = new Date(entered_value);

var permitted_date = new Date();
permitted_date.setDate(permitted_date.getDate());

if(entered_date <  permitted_date)
   {
      alert("Cannot be a date in the past");
      return false;
 }
});
</script>

This solution is designed for Submission form with AJAX enabled. Also, field name "Date0" used from your post.

Hope this helps.

 

Regards,

vitalikssssss

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...