Jump to content

Custom form validation (intercepting the form submission)

Recommended Posts

Posted

Has anybody had any experience with adding their own validation to a Squarespace form? Essentially stopping the normal submission process, checking some custom validation, then continuing as normal/stopping the submission if there was an invalid entry?

I have a client that needs some number fields to be submitted within a specific range and a few other variations on this theme. I'm hoping to find a simple solution, but I'm also happy to go with a very complicated one 😆

@michaeleparkour Do any of your plugins do this, or perhaps this is something you have come up against?

Posted

Tons of js validation libraries around... I have no ready-to-use simple solution for you, but the idea is simple: disable submit button on load, set all inputs to required,  listen inputs changes, validate values you need - if all good enable submit button.

Posted
15 hours ago, michaeleparkour said:

Tons of js validation libraries around... I have no ready-to-use simple solution for you, but the idea is simple: disable submit button on load, set all inputs to required,  listen inputs changes, validate values you need - if all good enable submit button.

@michaeleparkour You are a genius. Such a simple solution! I was totally over-complicating it in my head.

  • 2 months later...
Posted (edited)
13 hours ago, Tom_Warfield said:

Can you explain in a little more detail how to do those steps?  Disable submit button?  Listen for input changes?  Is this something that would require Developer Mode?  

Hi @Tom_Warfield.

This does not require Developer Mode, but you may need to upgrade your plan to enable Code Injection.

A basic example would be like this:

<script>
  //Specific form button to disable/re-enable
  const buttonToDisable = document.querySelector('[data-form-id="5ecfda33f13530766af10f1d"] input[type="submit"]');
  
  //Add 'disabled' attribute to the form button
  buttonToDisable.disabled = true;
  
  //Specific form input which needs custom validation
  let inputToValidate = document.querySelectorAll('[data-form-id="5ecfda33f13530766af10f1d"] input.text')[0];
  
  //Listen for changes to our input
  inputToValidate.addEventListener('input', function(e) {
    //Do your validation on every keystroke.
    //A basic example below. You could use a JS library for more complex validate.
    if(this.value) {
      //A value has been entered, re-enable button and continue with standard Squarespace submission
      buttonToDisable.disabled = false;
    }
  });
</script>

You would add this to your website's Footer by going to Settings  > Advanced > Code Injection.

You would need to further develop this example for a good user experience as the form button would do nothing until the user had added content to our specific input. But in principle, this would be a good starting point.

Edited by summitdigitaluk
Clearer instructions
  • 2 months later...
Posted
On 5/29/2020 at 6:28 AM, summitdigitaluk said:

Hi @Tom_Warfield.

This does not require Developer Mode, but you may need to upgrade your plan to enable Code Injection.

A basic example would be like this:


<script>
  //Specific form button to disable/re-enable
  const buttonToDisable = document.querySelector('[data-form-id="5ecfda33f13530766af10f1d"] input[type="submit"]');
  
  //Add 'disabled' attribute to the form button
  buttonToDisable.disabled = true;
  
  //Specific form input which needs custom validation
  let inputToValidate = document.querySelectorAll('[data-form-id="5ecfda33f13530766af10f1d"] input.text')[0];
  
  //Listen for changes to our input
  inputToValidate.addEventListener('input', function(e) {
    //Do your validation on every keystroke.
    //A basic example below. You could use a JS library for more complex validate.
    if(this.value) {
      //A value has been entered, re-enable button and continue with standard Squarespace submission
      buttonToDisable.disabled = false;
    }
  });
</script>

You would add this to your website's Footer by going to Settings  > Advanced > Code Injection.

You would need to further develop this example for a good user experience as the form button would do nothing until the user had added content to our specific input. But in principle, this would be a good starting point.

Where can I get that data-form-id from? I can't find it

  • 3 months later...
Posted (edited)

This is the only code I've tried that actually worked. And FYI, you can find data-form-id in developer mode in chrome when you are creating the custom form in products inventory.

Essentially, when you click add to cart and inject js code, our code executes before the form is shown. That's why it never finds the ID. Adding a settimeout, is the only way to get access to the elements within the custom form. No other way around it.

 

 var addtocart = document.getElementsByClassName("sqs-add-to-cart-button")[0];
    addtocart.style.backgroundColor="yellow";
  
  addtocart.onclick = function(){
  console.log("hello");

setTimeout(function() {     const buttonToDisable = document.querySelector('[data-form-id="5fb82d8ab2dc2c20b7469fe2"] input[type="submit"]');
    console.log(buttonToDisable); 

}, 1000);

 

Edited by ish2nv
Posted
On 8/20/2020 at 5:04 AM, Rodrigo218165 said:

Where can I get that data-form-id from? I can't find it

Hi Rodrigo,

My example refers to a Form Block. When you are editing the page you can find the data-form-id attribute by inspecting the code and looking for the correct form element. It can be a bit tricky whilst editing the page because there are extra elements laying over the top of all the blocks on the page which Squarespace use to show the edit buttons etc...

Screenshot-2020-11-27-at-10_07_56.thumb.jpg.2a88e364ef7191bcc340ff45d79f3e77.jpg

If you inspect the page outside of the editor, it is a bit easier to find:

Screenshot-2020-11-27-at-09_59_02.thumb.jpg.9ed9e1cfd03db0fc5b48facd6e836487.jpg

These examples are from 2 different websites incase you were wondering why the ID is different!

I hope this helps.

  • 1 month later...
Posted (edited)

Which part of the scrip is used to designate the field that requires validation?

I have a client that wants to validate a text field to only have states/provinces in a two Capital letter format.

<script>
  //Specific form button to disable/re-enable
  const buttonToDisable = document.querySelector('[data-form-id="5ecfda33f13530766af10f1d"] input[type="submit"]');
  
  //Add 'disabled' attribute to the form button
  buttonToDisable.disabled = true;
  
  //Specific form input which needs custom validation
  let inputToValidate = document.querySelectorAll('[data-form-id="5ecfda33f13530766af10f1d"] input.text')[0];
  
  //Listen for changes to our input
  inputToValidate.addEventListener('input', function(e) {
    //Do your validation on every keystroke.
    //A basic example below. You could use a JS library for more complex validate.
    if(this.value) {
      //A value has been entered, re-enable button and continue with standard Squarespace submission
      buttonToDisable.disabled = false;
    }
  });
</script>
Edited by JamesonW
  • 2 years later...
Posted

I have a similar challenge. I'm asking the user to find a "hidden word" somewhere in a long block of text. In the form, they enter their info (name, etc.) and also the extra word they found. I either want to disable the submit button until the user has entered the correct answer, or, on submitting with the wrong answer, return the user to the form with all the other fields retaining their values. An example of my form is here: https://www.riseto.org/agree-to-rules-validate. Anyone know how do do this? Thanks!

  • 10 months later...
Posted
On 11/27/2020 at 5:41 AM, summitdigitaluk said:

Hi Rodrigo,

My example refers to a Form Block. When you are editing the page you can find the data-form-id attribute by inspecting the code and looking for the correct form element. It can be a bit tricky whilst editing the page because there are extra elements laying over the top of all the blocks on the page which Squarespace use to show the edit buttons etc...

Screenshot-2020-11-27-at-10_07_56.thumb.jpg.2a88e364ef7191bcc340ff45d79f3e77.jpg

If you inspect the page outside of the editor, it is a bit easier to find:

Screenshot-2020-11-27-at-09_59_02.thumb.jpg.9ed9e1cfd03db0fc5b48facd6e836487.jpg

These examples are from 2 different websites incase you were wondering why the ID is different!

I hope this helps.

My form code has no data-form-id anywhere, nor the onsumbit js. is there something special I need to do?

what it currently shows

<div class="form-wrapper">
<form autocomplete="on" class="react-form-contents" novalidate="" data-success-redirect="">
<div></div>
<div class="field-list">
Posted (edited)
On 2/4/2024 at 1:58 PM, LMJ said:

My form code has no data-form-id anywhere, nor the onsumbit js. is there something special I need to do?

I do not have a solution but an observation.

The techniques discussed in this thread may no longer work since Squarespace redesigned forms on Oct. 8th 2023. It all just depends on what you are trying to accomplish and how you are going about it.

Squarespace moved from simple standard web forms to using React forms. React forms are much more difficult to impossible to customize as React ignores how standard web forms work and use their own methods to handle things.

If you try code in this thread and it doesn't seem to be working you may be running into how React forms work differently from standard web forms.

Edited by creedon
clarifications

Find my contributions useful? Please like, upvote, mark my answer as the best ( solution ), and see my profile. Thanks for your support! I am a Squarespace ( and other technological things ) consultant open for new projects.

Posted
2 hours ago, creedon said:

I do not have a solution but an observation.

The techniques discussed in this thread will no longer work since Squarespace redesigned forms on Oct. 8th 2023.

Squarespace moved from simple standard web forms to using React forms. React forms are much more difficult to impossible to customize as React ignores how standard web forms work and use their own methods to handle things.

Okay thanks for the info, thats really helpful. Thats kind of annoying because so much of the form is not screen reader optimised.

Posted

I can confirm that it is possible to do custom form validation on Squarespace forms, even after their switch to React. I can't post the solution we're using, but we are able to block form submission until custom validation rules are satisfied.

Just attach an onclick event to the form submit button, when clicked if the custom validation rules fail then use javascript to cancel the click event. With jQuery cancelling the submit button click event is something like this:

                  evt.preventDefault();
                  evt.stopPropagation();
                  evt.stopImmediatePropagation();
                  return false;
 
  • 4 months later...
Posted
On 2/5/2024 at 2:09 PM, Damien-O said:

I can confirm that it is possible to do custom form validation on Squarespace forms, even after their switch to React. I can't post the solution we're using, but we are able to block form submission until custom validation rules are satisfied.

Just attach an onclick event to the form submit button, when clicked if the custom validation rules fail then use javascript to cancel the click event. With jQuery cancelling the submit button click event is something like this:

                  evt.preventDefault();
                  evt.stopPropagation();
                  evt.stopImmediatePropagation();
                  return false;
 

Can you please provide more details? I am having trouble with jQuery to add eventlisteners, it does not seem like they are working because my console warnings are not shown in the console on "input".

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.