Site icon Hip-Hop Website Design and Development

validation field form registration vendor dokan woocommerce

I am making a marketplace with woocommerce and dokan. Seller registration is only for those who have an invitation code.

in the vendor-registration.php dokan template I add the input for the new field and in functions.php I add the code to validate. The problem I tried several ways to validate and none works

<p class="form-row form-group form-row-wide">
        <label for="shop-clave"><?php esc_html_e( 'Código de Invitación', 'dokan-lite' ); ?><span class="required">*</span></label>
        <input type="text" class="input-text form-control" name="clave" id="shop-clave" value="<?php if ( ! empty( $postdata['clave'] ) ) echo esc_attr($postdata['']); ?>" maxlength = "9" required="required" />
</p>

Some of the features I have tried

add_filter( 'woocommerce_registration_errors', 'wooc_validate_extra_register_fields', 10, 3 );

function wooc_validate_extra_register_fields( $errors, $username, $email ) {
    if ( isset( $_POST['clave'] ) && empty( $_POST['clave'] != '9634785' ) ) {
        $errors->add( 'clave', __( '<strong>Error</strong>: Código incorrecto!', 'woocommerce' ) );
        
    }

    return $errors;
}


add_action( 'woocommerce_after_checkout_validation', 'codigo_i', 10, 2);
 
function codigo_i( $fields, $errors ){
 
    if ( isset( $_POST['clave'] ) && empty( $_POST['clave'] != '9634785' ) ) {
        $errors->add( 'clave', __( '<strong>Error</strong>: Código incorrecto!', 'woocommerce' ) );
    }
}

add_filter( 'woocommerce_registration_errors', 'invite', 10, 3 );
  
function invite( $errors, $username, $email ) {
    if ( isset( $_POST['clave'] ) && empty( $_POST['clave'] != '9634785' ) ) {
        $errors->add( 'clave_error', __( '<strong>Error</strong>: Código incorrecto!', 'woocommerce' ) );
    }

    return $errors;
}

I also tried with js, add id (btn-enviar) to input submit

<script type="text/javascript">
const btnEnviar = document.getElementById('btn-enviar');

const validación = (e) => {
  e.preventDefault();
  const codigo = document.getElementById('shop_clave');
  if (shop_clave.value === "9634785") {
    alert("Código de invitación incorrecto");
    shop_clave.focus();
    return false;
  }
  
  return true;
}

submitBtn.addEventListener('click', validate);

</script>

but I can’t get one to work. any ideas!!