Validate woocommerce form from functions.php

0

I would like the input of the checkout form in wordpress to be validated with different rules, for example:

First and last name: only accept letters and accents Zip code: just accept numbers and only 5 your maxlegh Telephone: numbers only

Everything from the wordpress functions.php file

    
asked by Walworth Industrial de Vlvulas 27.02.2018 в 22:51
source

1 answer

0

One way you can validate the fields is by adding a method to:

woocommerce_checkout_process

So that in your functions.php you add a method, for example:

add_action('woocommerce_checkout_process', 'validatePhone');
function validatePhone() {
$billing_phone = filter_input(INPUT_POST, 'billing_phone');

 if (strlen(trim(preg_replace('/^[6789]\d{9}$/', '', $billing_phone))) > 0) {
    wc_add_notice(__('Número telefónico incorrecto.'), 'error');
 }
}

Another way to do it through the functions.php is to add your own Woocommerce fields.

Regarding your new question @Walworth to validate only letters you can change the regular expression or use the ctype_alpha ctype_alpha returns a value TRUE if its argument is a text string if you still decide to use a regular expression you can use:

if(preg_match('/^[a-zA-Z]+$/', $var)) //$var es la cadena a evaluar y retorna true si lo es
    
answered by 28.02.2018 / 03:52
source