[英]Express-validator .isEmail validator not working
我在驗證時遇到問題。 除了.isEmail,所有這些都在工作。 即使對於有效的 email 地址,它也一直說它是無效的。 無論我如何更改驗證,.isEmail 問題都不會 go 消失。 我正在使用最新版本的 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;
我遇到了同樣的問題,我通過直接使用 html 表單中的輸入字段名稱來解決它,而不是我在 js 文件中創建的這些輸入的變量名稱。 我看到您使用的“電子郵件”似乎是正確的,但也許您在 html 中有不同的名稱。
不正確的代碼: check('firstname').notEmpty();
正確代碼: check('name').notEmpty();
,考慮到我在 html 中有:
<label for="name">Name</label> <input type="text" id="name" name="name" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.