Jump to content

Defining a word limit in the contact form

Go to solution Solved by Squareko,

Recommended Posts

Posted

I would like to define a word / character limit for the message in the contact form so that a message can be a maximum of 400 characters long. Is there any help?

Das Meer ist blau. is a brand design agency based in Hamburg, since 2014. Our range of services includes: - full service campaign - corporate design | Brand-design - classic advertising - web design - magazine design - packaging design - merchandising - development of pos materials

  • Replies 3
  • Views 129
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

  • Solution
Posted

Add this code into code injection > footer

<script>
  document.addEventListener("DOMContentLoaded", function () {
    var messageField = document.querySelector('textarea[name="message"]');
    if (messageField) {
      messageField.addEventListener('input', function () {
        if (messageField.value.length > 400) {
          messageField.value = messageField.value.substring(0, 400); // Trims the message to 400 characters
        }
        // Optionally, display a warning if the message exceeds 400 characters
        var charCount = document.querySelector('#charCount');
        if (!charCount) {
          charCount = document.createElement('div');
          charCount.id = 'charCount';
          messageField.parentNode.appendChild(charCount);
        }
        charCount.innerText = `${messageField.value.length}/400 characters`;
      });
    }
  });
</script>

note: var messageField = document.querySelector('textarea[name="message"]'); in this line you may have to change the class name according to your targeted field.

 

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.