General

Using minlength and maxlength Attributes to Avoid Spam in Contact Form 7

Contact Form 7 is a popular plugin for creating forms in WordPress. While it is efficient and easy to use, it can sometimes be a target for spam submissions. One effective way to minimise spam is by adding the ‘minlength’ and ‘maxlength’ attributes to your form fields. These attributes allow you to define a minimum and maximum number of characters for text inputs, ensuring that users submit valid information.

How ‘minlength’ and ‘maxlength’ Work

  • ‘minlength’: Specifies the minimum number of characters required for a field. If the user submits input with fewer characters than this value, the form will display an error message.
  • ‘maxlength’: Specifies the maximum number of characters allowed for a field. Input exceeding this limit will trigger an error.

Benefits of Using These Attributes

  1. Reduce Spam: Many spam bots send very short or very long inputs, which often deviate from normal user behavior. Setting appropriate length limits can help filter out these submissions.
  2. Improve Data Quality: Ensures users provide inputs of reasonable length, enhancing the accuracy and reliability of the collected data.

How to Implement ‘minlength’ and ‘maxlength’ in Contact Form 7

To use these attributes, modify the relevant fields in your form shortcode. Here’s a step-by-step guide:

1. Open Your Contact Form

  1. In your WordPress dashboard, go to Contact > Contact Forms.
  2. Select the form you want to edit.

2. Modify the Form Fields

Add the ‘minlength’ and ‘maxlength’ attributes to your text-based fields. Below are examples for commonly used fields:

Example: Name Field

[text* your-name minlength:2 maxlength:50]

Example: Email Field

[email* your-email maxlength:100]

Example: Message Field

[textarea* your-message minlength:10 maxlength:500]

Testing the Form

After adding minlength and maxlength:
1. Submit test inputs to verify the error messages for too-short or too-long entries.
2. Adjust the character limits based on typical user behavior for your form.

By implementing minlength and maxlength, you can significantly reduce spam and ensure higher-quality submissions in your Contact Form 7 setup.

Was this guide helpful?

Need help with this?

Open a ticket and link to this guide so our team can see what you have tried.

View support tickets

Power your business with Ecenica Hosting

Built for WordPress and serious websites. Fast, secure and supported by real people in the UK.

Ecenica hosting services represented by a connected red route across London