javascriptwordpresswoocommercejquery-form-validator

How to check in javascript the checkout form Woocommerce before validation?


I would like to test if my form is valid or not (in Javascript). I can not find a way to do it in Javascript. I tried this :

if(checkoutForm.checkValidity()){
   console.log("Form is valid!");
}else{
   console.log("Form is invalid!");
}

But the problem with this is that it doesnt tell you if the ZIP code is good or not. I would like to use the same check form than Woocommerce.


Solution

  • My problem was the ZIP code. In JS, woocommerce doesn't verify if the ZIP match with the country. Woocommerce use the WC_validation to verify the ZIP code when you click "place older". Unfortunately I had to use PHP (call AJAX) with this WC_validation :

    if (WC_Validation::is_postcode( $postcode, $country ) ) {
          echo "ZIP Code OK";
    }else{
          echo "ZIP Code NOK";
    }