最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to normalize the input email? - Stack Overflow

programmeradmin7浏览0评论

I was working with express-validator when i encountered that i used normalize email for validation of email while signing up so i stored the normalized email to server.

Validation code:

router.post(
  "/signup",
  [
    check("name").not().isEmpty(),
    check("email").normalizeEmail().isEmail(),
    check("password").isLength({ min: 6, max: 15 }),
  ],
  userController.signupUser
);

Input email: [email protected]

normalized email: [email protected] (storing in db)

Now the problem is when working on login the input email doesn't matches and shows invalid credentials.

I was working with express-validator when i encountered that i used normalize email for validation of email while signing up so i stored the normalized email to server.

Validation code:

router.post(
  "/signup",
  [
    check("name").not().isEmpty(),
    check("email").normalizeEmail().isEmail(),
    check("password").isLength({ min: 6, max: 15 }),
  ],
  userController.signupUser
);

Input email: [email protected]

normalized email: [email protected] (storing in db)

Now the problem is when working on login the input email doesn't matches and shows invalid credentials.

Share Improve this question edited Jun 27, 2020 at 20:30 Vivek Sharma asked Jun 26, 2020 at 22:49 Vivek SharmaVivek Sharma 5536 silver badges20 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

You just need to pass options inside normalizeEmail () in your validation chain, in a middleware:

normalizeEmail({ gmail_remove_dots: false })

After researching and observing I noticed that the code which was used at the time of signing up can be used to normalize email at the time of login, I just had to add:

 router.post("/login",[check('email').normalizeEmail()], userController.loginUser);

After adding the email was converting to normalized one and can be used directly from requests.

You cannot remove any . or _ since it's part of the user's e-mail.

Here and example of validating an email address

发布评论

评论列表(0)

  1. 暂无评论