# Required fields custom validation message

**URL:** <https://community.gravityforms.com/t/required-fields-custom-validation-message/17671>\
**Category:** Get Help\
**Tags:** gform\_validation\_message\
**Created:** [June 13, 2024, 7:25am UTC](https://community.gravityforms.com/t/required-fields-custom-validation-message/17671 "2024-06-13T07:25:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![user5c9c8aebe9ed0529](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/user5c9c8aebe9ed0529/32/15135_2.png) [@user5c9c8aebe9ed0529](https://community.gravityforms.com/u/user5c9c8aebe9ed0529)\
**Post date:** [June 13, 2024, 7:25am UTC](https://community.gravityforms.com/t/required-fields-custom-validation-message/17671/1 "2024-06-13T07:25:20Z")

</div>

the custom validation message on required fields only dissaperas on pageload, meaning the validation message should be hidden WHEN the field is set… i tried this code snippet in functions.php

```auto
add_filter( 'gform_validation_message', 'henriette_gf_hide_validation_message', 10, 2 );

function henriette_gf_hide_validation_message( $message, $form ) {
  $is_valid = true; // Flag to track validation status

  // Loop through all fields in the form
  foreach ( $form['fields'] as $field ) {
    if ( $field['required'] && empty( $_POST["input_{$field['id']}"]) ) {
      $is_valid = false;
      break; // Exit loop if a required field is empty
    }
  }

  // If all required fields are filled, return an empty string (hides message)
  return $is_valid ? '' : $message;
}

```

did not work - then i tried this script

```auto
document.addEventListener("DOMContentLoaded", function() {
  const requiredFields = document.querySelectorAll(".gfield_required input, .gfield_required textarea");
  const validationMessage = document.querySelector(".gform_validation_message"); // Adjust selector if needed

  requiredFields.forEach(function(field) {
    field.addEventListener("input", function() {
      const hiddenField = document.getElementById("field_filled"); // Replace with your hidden field ID
      if (this.value.trim() !== "") {
        validationMessage.style.display = "none";
        hiddenField.value = "1"; // Update hidden field value
      } else {
        validationMessage.style.display = "block";
        hiddenField.value = "";
      }
    });
  });
});

```

Dis not work either…

---

<div class="post-metadata">

**Author:** ![faisalahammad](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/faisalahammad/32/13948_2.png) [@faisalahammad](https://community.gravityforms.com/u/faisalahammad)\
**Post date:** [June 15, 2024, 5:48pm UTC](https://community.gravityforms.com/t/required-fields-custom-validation-message/17671/2 "2024-06-15T17:48:33Z")

</div>

Hi Henriette,

You can follow my previous response on another ticket, where you’ll find the proper solution.

> [@This field is required. custom message](https://community.gravityforms.com/t/this-field-is-required-custom-message/16422/2):
>
> Hey, To add custom error/validation messages for each field, try the “Custom Error Messages for Gravity Forms” plugin, which is available for free. It will allow you to set custom messages like the one in the following screenshot. point_down [screenshot] Give it a try, and let me know how that goes! smile

Give it a try, and let me know how that goes! 😄

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/gravityforms1/original/2X/f/fef63f0599c45e0218c30d9f5d7ca8bff9b00189.png) [@system](https://community.gravityforms.com/u/system)\
**Post date:** [July 15, 2024, 5:48pm UTC](https://community.gravityforms.com/t/required-fields-custom-validation-message/17671/3 "2024-07-15T17:48:57Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
