【发布时间】:2020-04-03 22:04:33
【问题描述】:
大家好,我是 JavaScript 新手,我有一个问题。 我有 validator.js 文件,我在其中验证电子邮件、密码和请求。 就在这里
/**
* Simple regex validator for email
* @param email The email
* @returns {boolean} Whether the email matches the regex
*/
function email(email) {
const regExp = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return regExp.test(email);
}
/**
* Simple regex validator for password
* @param password The password
* @returns {boolean} Whether the password matches the regex
*/
function password(password) {
const regExp = /^(?=.*\d).{7,15}$/;
return regExp.test(password);
}
const request = {
email: {
message: '',
validate: function (req) {
const email = req.body.email;
if(!email){
email.message = 'Email is required';
return false;
}
if(!validator(email(email))){
email.message = 'Email is not valid';
return false;
}
return true;
}
}
};
module.exports = {
email,
password,
request
};
我想在电子邮件的验证功能中使用电子邮件(电子邮件)功能,但我有可变阴影,我该如何实现?
【问题讨论】:
-
没有两个同名的东西?例如。将电子邮件重命名为 validateEmail?
-
不要使用会影响函数名称的变量名称。改变一个或另一个。
标签: javascript variables scope shadow