这是电子邮件验证代码。
我只知道做例5,邮件长度必须超过5个字符串,否则,会变成错误,提醒人们重新写邮件。
我不知道如何根据邮件要求编写示例2-4。请给我一些建议。非常感谢!
function validateEmail(email) {
console.log(email)
// email requirements as below:
// email length >= 5 character
// @ cannot be the first character
// @ must before .
// . cannot be the last character
// example
// 1. a@a.com < return true
// 2. @a.com < return false, throw TypeError: invalid email address
// 3. a.b@com < return false, throw TypeError: invalid email address
// 4. a@a.com. < return false, throw TypeError: invalid email address
// 5. a@a.c < return false, throw TypeError: e-mail address too short
// 6. a@.com < return ture
// example 5, email length >= 5 character
if(email.length<5) {
throw new TypeError(`e-mail address too short`)
}
// example 4, . cannot be the last character
}
// example 1,6
return true
function newFunction() {
return "'"
}
您可以遵循此REGAX来验证电子邮件。请检查
const emailToValidate = 'a@a.com';
const emailRegexp = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
console.log(emailRegexp.test(emailToValidate));
感谢