I’m writing this code. It’s working when I’m entering numbers or special characters. But Even after entering only Chars, it’s showing the same validation. Can anyone please help me out how to validate the First name & Last Name field characters ony?
add_action( 'woocommerce_after_checkout_validation', 'misha_validate_fname_lname', 10, 2);
function misha_validate_fname_lname( $fields, $errors ){
if ( preg_match( '/^[a-zA-Z ]*$/', $fields[ 'billing_first_name' ] ) || preg_match( '/^[a-zA-Z ]*$/', $fields[ 'billing_last_name' ] ) ){
$errors->add( 'validation', 'Only characters required' );
}
}
2
Answers
I think you want to allow only alphabets in name fields in that case you can use ctype_alpha() Function
Code goes in functions.php tested & works
You can use the following code.
Few things worth noting
+
instead of*
as it will check for empty submissions.!
in your if condition.