How to update the following regular expression /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
to accept the "+" in the email address.
Now:
abc@gmail.com // true
abc+100@gmail.com // false
What I need
abc@gmail.com // true
abc+100@gmail.com // true
My Code:
export const handleEmailValidation = (email) => {
const validEmailAddress = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
const containAt = /^((?!@).)*$/;
const lastAt = /^[a-z|A-Z|0-9]+[^@]\s?@{1}$/;
if (containAt.test(String(email).toLowerCase())) {
return 'An email address must contain a single @ ';
}
if (lastAt.test(String(email).toLowerCase())) {
return 'Please enter a valid value after the @ ';
}
if (!validEmailAddress.test(String(email).toLowerCase())) {
return 'Please enter a valid email address';
}
return '';
};