【问题标题】:Express-validator .isEmail validator not workingExpress-validator .isEmail 验证器不工作
【发布时间】:2020-05-30 00:13:57
【问题描述】:

我遇到了验证问题。除了 .isEmail 之外,所有这些都正常工作。即使对于一个有效的电子邮件地址,它也一直说它是无效的。 无论我如何更改验证,.isEmail 问题都不会消失。我正在使用最新版本的 express-validator。

var express = require('express');
var router = express.Router();
var mongojs = require('mongojs');
const { check, validationResult } = require('express-validator');
var validator = require('validator');
var db = mongojs('registerapp', ['users']);
var bcrypt = require('bcryptjs');
var passport = require('passport'); //this could be used to incoporate facebook or twitter password 
for 
example
var LocalStrategy = require('passport-local').Strategy;

// Login Page - GET
router.get('/login', function(req, res) {
res.render('login');
});

// Register Page - GET
router.get('/register', function(req, res) {
res.render('register');
});

//adding the user POST
router.post('/register', [


/**/validation
check('name').notEmpty(),
check('email', 'Your email is required')
.isEmpty()
.isEmail().withMessage('Invalid email address') //The .isEmail issue won't go away no matter how I change the validation
.normalizeEmail(),
check('username').notEmpty(),
check('password', 'invalid password')
.isLength({ min: 1 })
.custom((value, { req, loc, path }) => {
    if (value !== req.body.password2) {
        // throw error if passwords do not match
        throw new Error("Passwords don't match");
    } else {
        return value;
    }
})**

], (req, res, next) => {
//check for errors
const errors = validationResult(req);
if (!errors.isEmpty()) {
    return res.status(422).json({ errors: errors.array() })
} else {
    console.log('success')
}

//Get form values
var name = req.body.name;
var email = req.body.email;
var username = req.body.username;
var password = req.body.password;
var password = req.body.password2;
})

module.exports = router;

【问题讨论】:

    标签: javascript node.js express express-validator


    【解决方案1】:

    我遇到了同样的问题,我通过直接使用 html 表单中的输入字段名称解决了这个问题,而不是我在 js 文件中创建的这些输入的变量名称。我看到您使用的“电子邮件”似乎是正确的,但也许您在 html 中有不同的名称。

    不正确的代码:check('firstname').notEmpty();

    正确代码:check('name').notEmpty();,考虑到我在 html 中:

    <label for="name">Name</label> <input type="text" id="name" name="name" />

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      • 2016-05-19
      • 2018-07-23
      • 2012-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多