What could be the Regex for accepting .@.
as a valid email id? Also need to check the below conditions.
Should contain exactly one
@
character.Before and after
@
should consist of at least 1 and at most 64 characters prising only letters, digits and/or dots(a-z, A-Z, 0-9, .)
.
was trying with \b[\w\.-]+@[\w\.-]+\.[\w]{0,0}
but not working as expected.
What could be the Regex for accepting .@.
as a valid email id? Also need to check the below conditions.
Should contain exactly one
@
character.Before and after
@
should consist of at least 1 and at most 64 characters prising only letters, digits and/or dots(a-z, A-Z, 0-9, .)
.
was trying with \b[\w\.-]+@[\w\.-]+\.[\w]{0,0}
but not working as expected.
-
2
/[a-z0-9.]{1,64}@[a-z0-9.]{1,64}/i
should do the trick (inside[]
brackets,.
does not need to be escaped, it's a litteral character) Regex101 explanation – blex Commented May 3, 2021 at 20:17 - 2 It's a waste of time, check the arobase, eventually a minimal length, but send an email with a confirmation link. – Casimir et Hippolyte Commented May 3, 2021 at 20:36
- @blex Can you post this as an answer? so that I can accept. – Hybrid Developer Commented May 5, 2021 at 17:34
2 Answers
Reset to default 4/^[a-z0-9.]{1,64}@[a-z0-9.]{1,64}$/i
should do the trick (Regex101 explanation):
function demo(str) {
const isEmail = /^[a-z0-9.]{1,64}@[a-z0-9.]{1,64}$/i.test(str);
console.log(isEmail, str);
}
demo('.@.'); // OK
demo('[email protected]'); // OK
demo('a-b_c@a_b'); // Bad chars
demo('a.b.c'); // No @
demo('1234567890123456789012345678901234567890123456789012345678901234@a'); // OK
demo('12345678901234567890123456789012345678901234567890123456789012345@a'); // Too long
Should contain exactly one @ character.
Before and after @ should consist of at least 1 and at most 64 characters prising only letters, digits and/or dots(a-z, A-Z, 0-9, .).
You don't need regex for that:
const validateEmail = (email) => {
const parts = email.split('@');
if (parts.length !== 2) {
// Either no @ or more than one
return false;
}
const [before, after] = parts;
return (
before.length > 0 &&
before.length <= 64
) && (
after.length > 0 &&
after.length <= 64
);
}
console.log(
validateEmail('.@.'),
validateEmail('[email protected]'),
validateEmail('wrong@'),
validateEmail('this@is@wrong'),
);
Using regex would be overkill, and will likely be a lot slower than performing 1 split, and 3 equality checks.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744685651a4587898.html
评论列表(0条)