WooCommerce Force Numeric Usernames On Registration

WooCommerce registration form showing an error is the username does not contain numbers only

Pre-Requisites

There are no pre-requisites in order for you to be able to implement this solution.

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About this Solution

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' );
	}
}
				
			

Snippet Benefits

  • Force WooCommerce usernames to only contain numbers otherwise show an error
WooCommerce Conversion Rate Optimisation (CRO) eBook
100 WooCommerce Conversion Rate Optimisation Tips

2 Responses

    1. 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 🙂

Leave a Reply

If you are going to write code in the comments, please wrap it between code tags.

Your email address will not be published. Required fields are marked *

Other Recent Guides

Subscribe To Emails

Get exclusive WooCommerce tips that I only share with email subscribers

Join hundreds of other subscribers