skip to Main Content

How to display required Field Errors “Inline” in account page Forms?

I have solution:
I can do this using jquery validator but this not WordPress way, there is any WordPress validtor to display or handle errors in inline.

I got answer for checkout page https://businessbloomer.com/woocommerce-display-required-field-errors-inline-checkout/
but this is not working for my account page forms.

2

Answers


  1. function.php

     add_filter( 'woocommerce_form_field', 'fields_in_label_error', 10, 4 );
    
    function fields_in_label_error( $field, $key, $args, $value ) {
       if ( strpos( $field, '</label>' ) !== false && $args['required'] ) {
          $error = '<span class="error" style="display:none">';
          $error .= sprintf( __( '%s is a required field.', 'woocommerce' ), $args['label'] );
          $error .= '</span>';
          $field = substr_replace( $field, $error, strpos( $field, '</label>' ), 0);
       }
       return $field;
    }
    

    In Style.css

    .woocommerce-checkout p.woocommerce-invalid-required-field span.error {
       color: #e2401c;
       display: block !important;
       font-weight: bold;
    }
    
    Login or Signup to reply.
  2. Beside the answer from pdchaudhary, I worked on a custom solution because I had the need for custom inline error messages (for custom or standard checkout fields) on my woocommerce shop.

    So I leave the link here to the corresponding gist. Maybe it’s helpful for people reading over this article.

    Gist: Woocommerce – Custom Inline Error Messages

    Cheers!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search