Jump to content

AJAX Deprecation-Proof my JS


Recommended Posts

Hi, all —

The upcoming deprecation of the "Disable AJAX loading" button affects some of my forms.  With AJAX loading, the following script (to require that forms be submitted three weeks in advance) will do nothing. It works without AJAX running, though. Here's what they all use:
 

<SCRIPT LANGUAGE="JavaScript">

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

var three_week_date = new Date();
three_week_date.setDate(three_week_date.getDate() + 20);

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

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

I believe the .onsubmit portion at the very end is the issue. Anyone know how to modify this so it will still work while AJAX is running? Thanks much!

BTW, I know 20 days isn't 3 weeks. My users can't seem to wrap their head around that, though. ;-)

Link to comment
Share on other sites

Hi @abitaquest

You can try this version:
 

<SCRIPT>

function check_date(e)

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

var three_week_date = new Date();
three_week_date.setDate(three_week_date.getDate() + 20);

if(entered_date < three_week_date)
   {
      e.preventDefault()
      alert("Must be at least 3 weeks in future");
      return false;
 }
}

document.addEventListener('BeforeFormSubmit', check_date)
</SCRIPT>

Let me know if it worked

Link to comment
Share on other sites

Thanks very much for that.

With AJAX loading allowed, it does show the error message briefly ("Must be at least 3 weeks," etc.), but then it goes right ahead and submits the form anyway.

With AJAX loading disabled, it submits, and no error message appears.

Got an idea for stopping the submit from happening when an error appears?

Link to comment
Share on other sites

For anyone else affected by this particular change, here is the working script and notes, courtesy of the Caspio folks.
 

Quote


For the script to check the value of 'InsertRecorddateNeeded' before submission, you can use this code instead:
 

<script>

document.addEventListener('BeforeFormSubmit', function check_date(e) {

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

var three_week_date = new Date();
three_week_date.setDate(three_week_date.getDate() + 20);

if(entered_date < three_week_date)
{
e.preventDefault()
alert("Must be at least 3 weeks in future");
return false;
}

});

</script>


It would be better to call the event first so that when the DataPage loads, it will run the first syntax. For more information on this event, you can check this article: https://howto.caspio.com/datapages/ajax-loading/#:~:text=%7D)%3B%0A%3C/script%3E-,Example%203,-%3A%20A%20JavaScript

Make sure to disable the HTML Editor in the Advanced tab before pasting the code to avoid additional spaces or characters: https://howto.caspio.com/datapages/datapage-components/disabling-html-editor-in-datapage-header-footer-and-html-blocks/

 

 

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...