我是 Mongoose 的新手...我在下面有一个代码片段。如果我想在保存之前检查电子邮件中 @ 符号后面的内容,我将如何进行验证?
var user = new User ({
firstName: String,
lastName: String,
email: String,
regDate: [Date]
});
user.save(function (err) {
if (err) return handleError(err);
})
最佳答案
您可以使用正则表达式。看看这个问题:Validate email address in JS?
你可以试试这个
user.path('email').validate(function (email) {
var emailRegex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
return emailRegex.test(email.text); // Assuming email has a text attribute
}, 'The e-mail field cannot be empty.')
关于javascript - 如何在 Mongoose 中验证电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34399118/