7/19/2023 0 Comments Expressvalidator![]() In the code above first we validate the form input, if there are any errors we will render the user to the same index page else we will render the user to the home page.Here i assume that you have index.jade, home.jade files in your view folder. Req.assert('password', 'Please enter password').notEmpty() Req.assert('email', 'Please enter a valid email').isEmail() Req.assert('name', 'Please enter name').notEmpty() Inside the post we do our validation with the following lines: ![]() Next we add two routes, a get and a post. Var expressValidator = require('express-validator') Īpp.use(expressValidator()) // This line should be after express.bodyParser()! Now open your app.js and add the following code: var express = require('express') Lets say we have a registration form and we are going to use express-validator to validate the user input.įirst we create an application using express generator with the commands below. It has its own validation rule, and if any of the applied validation rule is not satisfied, it will populate the response object with errors and return as a response.We can use this response in our view template to show the errors to users. ![]() Express-validator is a node's middleware that can be used as form validation or we can say to validate the body, params, query, headers and cookies of request object.
0 Comments
Leave a Reply. |