Jump to content

Java Script Prevent Submission


Recommended Posts

Hi,

I've been fiddeling with preventing a submissionform to submit and I have seen a few solutions in the forum. However it seems i cant get those to work, perhaps because i try to rewrite them a bit to fit my purpose.

This is what I am trying to achieve:

  • I have two calculated virtual fields. These display the text WARNING if other fields exceed some values. (This works)
  • If the two Virtual Fields display the warnings I want the form to prevent submission and high-light with alert the existing warnings.

Below script prevent submission completely regardless if there are warnings in the virtual fields or not... and there are no alert either.:

 

<script type="text/javascript">
document.addEventListener('BeforeFormSubmit', function(event) {
event.preventDefault();

var Input1 = document.getElementByName('cbParamVirtual1').value;
var Input2 = document.getElementByName('cbParamVirtual2').value;
     if (Input1 === 'WARNING' || Input2 === 'WARNING') {
alert('Enter acceptable volume!');

} else {

document.forms["caspioform"].submit();

}

});
</script>

 

I would appreciate if anyone could point me in the right direction, getting the script to work.

Thanks a lot in advance.

Sincerely

Ras

 

Link to comment
Share on other sites

Hi @Ras2019,

The way you trying to refer to Virtual fields is incorrect. 

I would recommend using query selectors with contains type of match.

This code should work fine:

<script type="text/javascript">

document.addEventListener('BeforeFormSubmit', function(event) {

var Input1 = document.querySelector("span[id^='cbParamVirtual1']").innerHTML;

var Input2 = document.querySelector("span[id^='cbParamVirtual2']").innerHTML;

     if (Input1 === 'WARNING' || Input2 === 'WARNING') {
event.preventDefault();

alert('Enter acceptable volume!');


} else {

document.forms["caspioform"].submit();

}

});
</script>

Regards,

vitalikssssss

Link to comment
Share on other sites

  • 1 year later...
  • 2 years later...

Hi - Just to add, this code also works:

<script type="text/javascript">

document.addEventListener('BeforeFormSubmit', function(event) {

var Input1 = document.querySelector("input[id^='cbParamVirtual1']").value;

if (Input1 == '') {
event.preventDefault();
alert('Invalid Submission!');

} else {

document.form["caspioform"].submit();

}

});
</script>
Link to comment
Share on other sites

  • 4 months later...

Hi, 

Just want to share. You can also use this code if you want to display the error message inside HTML block tags instead of an alert.
 

<script type="text/javascript">
document.addEventListener('BeforeFormSubmit', function(event) {
  var Input1 = document.querySelector("input[id^='cbParamVirtual1']").value;
  var errorMessageElement = document.getElementById('error-message'); // Replace 'error-message' with the actual ID of your <p> block
  
  if (Input1 == '') {
    event.preventDefault();
    errorMessageElement.innerHTML = 'Invalid Submission!';
  } else {
    // Clear the error message if it was previously displayed
    errorMessageElement.innerHTML = '';
    document.form["caspioform"].submit();
  }
});
</script>

 

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