Reputation: 131
I have the javascript below. I am trying to put in place password validation rules to have the following:
Could I please get pointers on how to get the uppercase & special characters validations to work?
// Register Handle
router.post('/register', (req, res) => {
const { name, email, password, password2, userType } = req.body;
let errors = [];
// Check required fields
if(!name || !email || !password || !password2 || !userType){
errors.push({ msg: 'Please fill in all fields'})
}
// Check if passwords match
if(password !== password2) {
errors.push({ msg: 'Passwords do not match' });
}
// Check password length
if(password.length < 12) {
errors.push({ msg: 'Password should be at least 12 characters long' });
}
// Check if password contains at least 1 letter
if(password.search(/[a-z]/i) < 0){
errors.push({ msg: 'Password should contain at least one letter' });
}
// Check if password contains at least 1 Uppercase letter
if(password.search(/[A-Z]/i) < 0){
errors.push({ msg: 'Password should contain at least one uppercase character' });
}
// Check if password contains at least 1 number
if(password.search(/[0-9]/) < 0){
errors.push({ msg: 'Password should contain at least one number' });
}
// Check if password contains at least 1 special character
if(password.search() < 0){
errors.push({ msg: 'Password should contain at least one number' });
}
if(errors.length > 0) {
res.render('register', {
errors,
name,
email,
password,
password2,
userType
})
} else {
// Passed Validation
User.findOne({ email: email })
.then(user => {
if(user) {
// User exists
errors.push({ msg: 'Email already registered' });
res.render('register', {
errors,
name,
email,
password,
password2,
userType
})
} else {
const newUser = new User({
name,
email,
password,
userType
});
// Hash Password
bcrypt.genSalt(10, (err, salt) => bcrypt.hash(newUser.password, salt, (err, hash) =>{
if(err) throw err;
// Sets password to hash
newUser.password = hash;
// Save user
newUser.save()
.then(user => {
req.flash('success_msg', 'Registration successful! Login to continue')
res.redirect('/auth/login')
return;
})
.catch(err => console.log(err));
}))
}
})
}
});
Upvotes: 1
Views: 271
Reputation: 18631
Letter and digit checks should be updated as follows:
// Check if password contains at least 1 letter
if(/[a-z]/i.test(password)){
errors.push({ msg: 'Password should contain at least one letter' });
}
// Check if password contains at least 1 Uppercase letter
if(/[A-Z]/.test(password)){
errors.push({ msg: 'Password should contain at least one uppercase character' });
}
// Check if password contains at least 1 number
if(/[0-9]/.test(password)){
errors.push({ msg: 'Password should contain at least one number' });
}
Better use test
instead of search
when checking if an expression can match anything in the string, and /[A-Z]/i
matches any letters case-insensitively.
P.S. If you want to check special characters see Check for special characters in string. There are heaps of ways to match special characters in JavaScript.
Upvotes: 2