Customizing Unique Validation Messages for Specific Forms

Would you like to customize the unique validation message for a specific form in WPForms? While WPForms allows you to customize validation messages globally, you might want to have different messages for different forms.

In this tutorial, we’ll show you how to use a PHP code snippet to change the unique validation message for a specific form.

Adding the Code Snippet

To customize the unique validation message for a specific form, you’ll need to add the following PHP code snippet to your site. If you need help with adding code snippets to your site, please take a look at this tutorial.

add_action( 'wpforms_wp_footer_end', 'wpf_custom_unique_validation_message' );
function wpf_custom_unique_validation_message() {
    ?>
    <script type="text/javascript">
    document.addEventListener('DOMContentLoaded', function() {
        // Target the specific form by ID
        var form = document.querySelector('#wpforms-100');
        if (form) {
            function replaceErrorMessage() {
                var errorMessages = form.querySelectorAll('.wpforms-error');
                errorMessages.forEach(function(messageElement) {
                    if (messageElement.textContent.includes('The value must be unique.')) {

                    messageElement.textContent = 'This value is already in use. Please enter a unique value.';
                    }
                });
            }
            replaceErrorMessage();
            new MutationObserver(replaceErrorMessage).observe(form, {
                childList: true,
                subtree: true
            });
        }
    });
    </script>
    <?php
}

Customizing the Code

To make this code work for your specific form, you’ll need to make a few adjustments:

  1. Replace #wpforms-100 with the ID of your specific form. For any help in finding your ID numbers, please check out this tutorial.
  2. Modify the custom error message in this line: messageElement.textContent = 'This value is already in use. Please enter a unique value.'; Replace the text with your desired custom message.

And that’s it! You’ve now customized the unique validation message for a specific form.

Next, would you like to learn more about customizing form validation? Check out our tutorial on how to customize WPForms validation messages.