I am trying to create a custom Regex to validate emails that follow the rules stated below:
- Email should start with a alphabet (a-z) and should not start with special characters or numeric values
- after the first letter, it could contains numbers(0-9), letters(a-z), underscores(_) and dot(.)
- it should have only one @ symbol in it
- after the @ symbol, it should only allow to contain alphabets and dot (no more special characters or numbers other than dot(.))
- email should not end with dot(.) but rather end with an alphabet
- it should not have any whitespace anywhere
I have two arrays: trueEmails
contains valid emails and notEmails
contains invalid emails.
I created the following Regex:
const email = /^[a-zA-Z] (\d|.|\w)*@[a-zA-Z] .[a-zA-Z] .*[a-zA-Z] $/;
My Regex is not working for rule no. 2, 3, 4 and 6. Here is my code.
const notEmails = [
// rule 1
'[email protected]',
'#[email protected]',
'[email protected]',
// rule 2
'test&[email protected]',
// rule 3
'test@[email protected]',
// rule 4
'[email protected]',
// rule 5
'[email protected].',
// rule 6
'white [email protected]'
]
const trueEmails = [
// rule 1
'[email protected]',
// rule 2
'[email protected]',
'[email protected]',
'[email protected]',
// rule 3
'[email protected]',
// rule 4
'[email protected]',
// rule 5
'[email protected]',
// rule 6
'[email protected]'
]
const email = /^[a-zA-Z] (\d|.|\w)*@[a-zA-Z] .[a-zA-Z] .*[a-zA-Z] $/;
console.log("NotEmails, should return false")
console.log(notEmails.map((each) => each ' => ' email.test(each)));
console.log("trueEmails, should return true")
console.log(trueEmails.map((each) => each ' => ' email.test(each)));
Thanks in advance.
CodePudding user response:
I've updated the regex to work for you. It's not fool proof but works for the restrictions you've put in place.
const notEmails = [
// rule 1
'[email protected]',
'#[email protected]',
'[email protected]',
// rule 2
'test&[email protected]',
// rule 3
'test@[email protected]',
// rule 4
'[email protected]',
// rule 5
'[email protected].',
// rule 6
'white [email protected]'
]
const trueEmails = [
// rule 1
'[email protected]',
// rule 2
'[email protected]',
'[email protected]',
'[email protected]',
// rule 3
'[email protected]',
// rule 4
'[email protected]',
// rule 5
'[email protected]',
// rule 6
'[email protected]'
]
const email = /^[a-zA-Z] [a-zA-Z0-9_.] @[a-zA-Z.] [a-zA-Z]$/;
console.log("NotEmails, should return false")
console.log(notEmails.map((each) => each ' => ' email.test(each)));
console.log("trueEmails, should return true")
console.log(trueEmails.map((each) => each ' => ' email.test(each)));
Description
Regex: /^[a-zA-Z] [a-zA-Z0-9_.] @[a-zA-Z.] [a-zA-Z]$/
^
Start of line[a-zA-Z]
any character from a-z[a-zA-Z0-9_.]
any character from a-z, and number, underscores and periods.@
matches literal @ sign[a-zA-Z.]
any character from a-z and periods- [a-zA-Z]` any character from a-z
$
end of line
CodePudding user response:
I'd suggest a case-insensitive search.
And not allowing 2 connected dots.
/^(?!.*[.]{2})(?=[a-z])[\w.]*[a-z0-9]@[a-z][a-z.]*[.][a-z]{2,63}$/i
const notEmails = [
// rule 1
'[email protected]',
'#[email protected]',
'[email protected]',
// rule 2
'test&[email protected]',
// rule 3
'test@[email protected]',
// rule 4
'[email protected]',
// rule 5
'[email protected].',
// rule 6
'white [email protected]'
]
const trueEmails = [
// rule 1
'[email protected]',
// rule 2
'[email protected]',
'[email protected]',
'[email protected]',
// rule 3
'[email protected]',
// rule 4
'[email protected]',
// rule 5
'[email protected]',
// rule 6
'[email protected]'
]
const email = /^(?!.*[.]{2})(?=[a-z])[\w.]*[a-z0-9]@[a-z][a-z.]*[.][a-z]{2,63}$/i;
console.log("NotEmails, should return false")
console.log(notEmails.map((each) => each ' => ' email.test(each)));
console.log("trueEmails, should return true")
console.log(trueEmails.map((each) => each ' => ' email.test(each)));