Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email regex pattern in Nodejs [closed]

Tags:

node.js

I want regex to validate Email Address, which rejects the email addresses like [email protected] or [email protected] or Raman [email protected].
It allow the emails which are containing at least one character or 'combination of number & characters' for example:- [email protected], [email protected], [email protected]

like image 524
Kumar Avatar asked Mar 18 '20 10:03

Kumar


Video Answer


1 Answers

The validation function I use:

function isEmail(email) {
    var emailFormat = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;
    if (email !== '' && email.match(emailFormat)) { return true; }
    
    return false;
}

However, in your specific case, to further filter out cases like '[email protected]' and '[email protected]', the regexp shall be modified a bit into:

var emailFormat = /^[a-zA-Z0-9_.+]*[a-zA-Z][a-zA-Z0-9_.+]*@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;

or, more elegantly:

var emailFormat = /^[a-zA-Z0-9_.+]+(?<!^[0-9]*)@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;

Reference: Regex: only alphanumeric but not if this is pure numeric

like image 109
zhugen Avatar answered Oct 19 '22 13:10

zhugen