I made a register page using node js and I made some validations as an example if password doesn't match with confirm the passwords it returns back to the signup page and listing the errors beneath the the (h1) tag which is sign up but it's returns back to the sign page without any errors here's my code:
module.exports.signup_post = (req,res)=>{
let {firstName,lastName,username,email,password,password2} = req.body;
console.log(
firstName,
lastName ,
username,
email,
password,
password2
);
let errors =[];
if (!firstName || !lastName || !username || !email || !password || !password2){
errors.push({message:'please enter the all fields'});
}
if (password.length<6){
errors.push({message:'password should be at least 6 characters'})
}
if (password!= password2){
errors.push({message:'passwords do not match'})
}
if (errors.length > 0){
res.render('register');
}else{
res.render('login')
}
}
<ul>
<% if (typeof errors != 'undefined'){ %>
<% errors.forEach(error =>{ %>
<li><% error.message %></li>
<% }) %>
<% } %>
</ul>
CodePudding user response:
Change the fifth line from the bottom to:
res.render("register", { errors: errors });