This code snippet shows you how to make the registration form on the My Account page show an error is a user defined username is not numeric. This guide is particularly useful if you would like usernames to be an ID which already exists in the business.
/**
* Snippet Name: WooCommerce Force Numeric Usernames On Registration
* Snippet Author: ecommercehints.com
*/
add_action( 'woocommerce_register_post', 'ecommercehints_force_numeric_username', 10, 3 );
function ecommercehints_force_numeric_username( $username, $email, $errors ) {
if ( !is_numeric($username) ) {
$errors->add( 'numeric_username_error', 'Your username must be numeric only' );
}
}
2 Responses
Hi,
Is possible for this to also make it required to have only 10 digits.
Hey Roberto,
It sure is – checkout the PHP function strlen()
If you still have trouble, contact us for a quote and we’ll be happy to write a custom snippet just for you 🙂