【问题标题】:Email validation coding in node.is [duplicate]node.is中的电子邮件验证编码[重复]
【发布时间】: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


【解决方案1】:

您可以按照此 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));

谢谢

【讨论】:

    猜你喜欢
    • 2011-07-16
    • 2011-06-21
    • 2013-09-25
    • 2012-05-20
    • 2013-01-18
    • 2013-02-12
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多