Created
May 20, 2026 07:14
-
-
Save xlplugins/8272118c3e4f20fc9c066c967b7cea9b to your computer and use it in GitHub Desktop.
Validate phone field for 9 digits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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