Block Specific Characters in a User's Email Address During Registration
Last Updated:
Overview
This article clarifies whether it is possible to block specific characters from an email address during user creation. A user's email address contains invalid characters, such as ?, /, or #, preventing its use during registration.
Applies To
- User registration
- Pre-User-Registration Trigger
- Actions
Solution
A direct way to block specific characters from an email address is not available in the Auth0 dashboard user interface (UI). This functionality is achievable using a Pre-User-Registration Trigger in Actions.
Use the following sample code for the Action:
/**
* @param {Event} event - Details about the user and the context of the registration.
* @param {PreUserRegistrationAPI} api - Interface to modify the context of the registration.
*/
exports.onExecutePreUserRegistration = async (event, api) => {
const email = event.user.email;
if (!email) {
return;
}
const forbiddenCharsRegex = /[?;/"\[\]#]/;
const hasForbiddenChars = forbiddenCharsRegex.test(email);
if (hasForbiddenChars) {
api.validation.error(
"invalid_email_format",
"Your email address contains invalid characters and cannot be used."
);
}
};
This code snippet blocks characters such as ?, ;, /, ", [, ], and #.