【发布时间】:2021-12-10 23:05:08
【问题描述】:
这是电子邮件验证编码。
我只知道做示例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 "'"
}
【问题讨论】:
-
验证电子邮件地址是一个臭名昭著且令人惊讶的难题。如果您正在构建生产系统,最好使用完全调试过的包。如果是学生练习:正则表达式或解析器即将出现。
-
您可能需要查看
String.prototype.indexOf()以检索字符串中单个字符的位置
标签: javascript node.js substring email-validation