Jump to content

Stop Form Submission Based On Calculated Field


Recommended Posts

Hello All,

I am trying to create an account creation datapage that has a built in checker to make sure the last four digits of the social security number entered matches the last four in our system.

To do this, I have already created a calculated field, which gives me a YES or NO depending on if the last four digits entered on the social security match what we have on file. 

Here is that code:
 

CASE WHEN

(SELECT COUNT(*) FROM CollectionCRM WHERE Filenumber=[@field:CustomerTable_FileNumber] AND LastFour=[@field:CustomerTable_LastFour]) > 0

THEN 'YES'

ELSE
'NO'

END

I am a little stuck now. What I am trying to do is when the calculated field says YES the account is created, if it says NO, an alert is presented that says "You are not authorized to create this account." Here is what I have tried:
 

<script>

document.querySelector('#caspioform').onsubmit = function(e) {
    e.preventDefault();

var input1= document.getElementById("LastFourCheck").value;


if (input1  =="NO"){
alert('You are not authorized to create this account.');
} 


else {
this.submit();

}

}

</script>

Could somebody take a look and let me know what I am doing wrong? I hope you guys can help me out, thanks so much!

Link to comment
Share on other sites

Hello @rhinger,

As far as I understand this is a Submission Form DataPage ,CASE statement returns correct value and you need to fix only the JavaScript code.

1) Please use the BeforeFormSubmit Caspio event handler. 

2) If you set the "LastFourCheck" field as a Calculated Value Form Element as shown on the screenshot

siDsrhv.png

then this input field on the Submission form has id that starts from InsertRecord prefix and has dynamic so-called uniqueSuffix.

Here is the example:

OUtlVPF.png

To get the value of this input field, please use the syntax:  document.querySelector("input[id*='InsertRecordLastFourCheck']").value

Please try to add this code in the Footer section and test:

<script>
  document.addEventListener('BeforeFormSubmit', function(event) {

     let checkValue = document.querySelector("input[id*='InsertRecordLastFourCheck']").value;
    
     if (checkValue == 'NO') {
     event.preventDefault();
     alert("You are not authorized to create this account.");
    }
  })
</script>

You may want to check these articles:

https://forums.caspio.com/topic/4377-js-guide-caspio-form-elements/

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

Please update this thread if you have additional questions.

Link to comment
Share on other sites

Hi @CoopperBackpack,

Thanks for your response, I appreciate you taking your time to give me such a detailed response and references.

I did go ahead and deploy the code and get it to work as intended.  I had to alter it slightly due to me using a view, but that was no issue.

Thanks again.

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

×
×
  • Create New...