Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save xlplugins/8272118c3e4f20fc9c066c967b7cea9b to your computer and use it in GitHub Desktop.

Select an option

Save xlplugins/8272118c3e4f20fc9c066c967b7cea9b to your computer and use it in GitHub Desktop.
Validate phone field for 9 digits
add_action( 'woocommerce_checkout_process', 'validate_billing_phone_9_digits' );
function validate_billing_phone_9_digits() {
$phone = isset( $_POST['billing_phone'] ) ? sanitize_text_field( $_POST['billing_phone'] ) : '';
// Remove all spaces and non-digit characters before checking
$phone_digits = preg_replace( '/\D/', '', $phone );
if ( strlen( $phone_digits ) !== 9 ) {
wc_add_notice(
__( 'Please enter a valid 9-digit phone number.', 'woocommerce' ),
'error'
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment