0

I am having a bit of a challenge with Woocommerce registration form. Currently, users are able to register using all numeric characters as their usernames. I can't think of a way to enforce that they use only alphanumeric combinations. Not only numbers. I can't seem to find any answers anywhere.

1 Answer 1

1

You can use the woocommerce_registration_errors hook to validate the username & set an error if your conditions aren't met - WooCommerce will bail and pass the error back to the UI for the user:

add_filter( 'woocommerce_registration_errors', function ( WP_Error $errors, $username ) {
    if ( ! preg_match( '/[a-z]/i', $username ) ) {
        $errors->add( 'username', 'Username must contain alphabetic characters.' );
    }

    return $errors;
}, 10, 2 );
2
  • Hi! Thank you for your response. forgive me, but I am relatively intermediate with programming. Does this code chunk go into functions.php?
    – Cornelius
    Commented Feb 4, 2019 at 14:23
  • Yes you can pop it in your functions.php. If you want to get a bit more "fancy", pop it in a MU plugin - this helps keep your logic separated from your theme. Commented Feb 4, 2019 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.