我有一个mongoose用户模式(UserSchema),我想验证邮箱是否有正确的语法.我目前使用的验证如下:
UserSchema.path('email').validate(function (email) {
return email.length
}, 'The e-mail field cannot be empty.')
但是,这只判断字段是否为空,而不判断语法.
是否已经存在我可以重复使用的东西,或者我必须想出自己的方法并在validate函数中调用它?
我有一个mongoose用户模式(UserSchema),我想验证邮箱是否有正确的语法.我目前使用的验证如下:
UserSchema.path('email').validate(function (email) {
return email.length
}, 'The e-mail field cannot be empty.')
但是,这只判断字段是否为空,而不判断语法.
是否已经存在我可以重复使用的东西,或者我必须想出自己的方法并在validate函数中调用它?
你可以使用正则表达式.看看这个问题:Validate email address in JavaScript?
我以前用过这个.
UserSchema.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.')