NodeJS 如何格式化和验证电子邮件节点js

w46czmvw  于 2023-10-17  发布在  Node.js
关注(0)|答案(5)|浏览(145)

我希望电子邮件的格式如下:email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)。
做这件事的最好方法是什么?
我有一个注册组件,我有这样的字段:

<mat-form-field>
              <input matInput placeholder="Email" name="email" [(ngModel)]="email" required>
</mat-form-field>

在我的usersRouter中,我有注册功能:

router.post('/users/register', (req, res) => {
        ...
        const user = new User({
            ...
            email: req.body.email,
            ...
        });
     ...
});

此外,我使用mongo,在UserSchema中,我为电子邮件设置了以下内容:

email: {
        type: String,
        required: true
}

谢谢你,谢谢

ewm0tg9j

ewm0tg9j1#

您可以使用电子邮件验证模块:

var validator = require("email-validator");
validator.validate("[email protected]");

或者,如果你不想要任何依赖:

var emailRegex = /^[-!#$%&'*+\/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+\/0-9=?A-Z^_a-z`{|}~])*@[a-zA-Z0-9](-*\.?[a-zA-Z0-9])*\.[a-zA-Z](-?[a-zA-Z0-9])+$/;

function isEmailValid(email) {
    if (!email)
        return false;

    if(email.length>254)
        return false;

    var valid = emailRegex.test(email);
    if(!valid)
        return false;

    // Further checking of some things regex can't handle
    var parts = email.split("@");
    if(parts[0].length>64)
        return false;

    var domainParts = parts[1].split(".");
    if(domainParts.some(function(part) { return part.length>63; }))
        return false;

    return true;
}

资料来源:
https://www.npmjs.com/package/email-validator
https://github.com/manishsaraan/email-validator/blob/master/index.js

ctehm74n

ctehm74n2#

使用正则表达式类似这样:
解决方案1:

^[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])?)*$
  • 示例代码:*
const emailToValidate = '[email protected]';
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));

解决方案二:
因为你使用angular,所以你可以在前端使用Validators.email来验证邮件。
如果你在这里查看Validators.email的angular源代码,你会发现一个EMAIL_REGEXP常量变量,其值如下:

/^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[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])?)*$/;

你也可以在后端使用它来验证输入。

iibxawm4

iibxawm43#

我喜欢validator.js
从docs:

var validator = require('validator');

validator.isEmail('[email protected]');

来源:https://www.npmjs.com/package/validator

db2dz4w8

db2dz4w84#

在我的项目中,我使用了电子邮件验证模块

npm i email-validator

const emailvalidator = require("email-validator");
if(emailvalidator.validate(req.body.email)){
      // Your call to model here
}else{
   res.status(400).send('Invalid Email');
}

您可以从下面的链接安装该模块
https://www.npmjs.com/package/email-validator

92dk7w1h

92dk7w1h5#

我喜欢在图书馆里看这些。使您能够轻松验证各种事物!

const Joi = require('joi');

const schema = Joi.string().email().trim();

// "[email protected]"
const validEmail = Joi.attempt('[email protected]', schema);

// trimmed "[email protected]"
const validEmail = Joi.attempt(' [email protected] ', schema);

// throws error
const validEmail = Joi.attempt('john@doe', schema);

更多关于Joi:https://joi.dev/api/

相关问题