Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8568917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:12:51+00:00 2026-06-11T18:12:51+00:00

I am validating my form with express-validator…if I have errors, what’s the best way

  • 0

I am validating my form with express-validator…if I have errors, what’s the best way to stop the form from submitting and go back to the login page. This is what I have, which works, just looking for best way to handle this (not sure about the return statement).

//routes/index.js
exports.login.post = function(req, res){
  req.assert('username', 'Please enter username').notEmpty();
  req.assert('password', 'Please enter password').notEmpty();
  res.locals.errors = req.validationErrors(true);

  if ( res.locals.errors ) {
    console.log(res.locals.errors);
    var d = { title: 'Login' };
    res.render('login', { d: d });
    return;
  }

 //do login here if no errors
};

I also do not know how to get at original values in form template (ejs).

I tried passing req.param in, but it is empty.

The err object only has those fields with errors, so if I enter username, but leave password blank, I only get password in err object, not username…so I need req.param.username, but when I pass { q: req.param} and use <%= inspect(q) %> it is empty.

update: req.body gives me the original post parameters to pre-populate the form. so you need err stack and req.body stack to have a good experience.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:12:52+00:00Added an answer on June 11, 2026 at 6:12 pm

    This is what I do

    app.post('/form_process', validateForm, form.handle)
    function validateForm(req, res, next) {
      req.assert('username', 'Invalid Username').notEmpty().isAlpha();
      req.assert('password', 'Invalid Password').notEmpty().isAlpha();
    
      var errors = req.validationErrors();
      if (errors) {
        //res.send(errors,500);
        var original_values = {
          username: req.param('username'),
          password: req.param('password')
        }
        res.render('login', { errorMsg: errors, original: original_values, title: "Login error" })
        return;
      }
      else next()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two submit handlers, one validating a form and one submitting the form:
I'm validating a form using the Validator interface with Spring MVC 3.0.2 using SimpleFormController
I am validating my form using SSJS from a submit button. To display the
I'm validating a login form with jQuery AJAX call to PHP. In php, I
First of all, I have already readed this: Validating form dropdown in CodeIgniter But
i have this simple JS for validating form, can someone tell me how to
In form validating,I find such codes if(isset($_POST['ajax']) && $_POST['ajax']==='login-form') { echo CActiveForm::validate($model); Yii::app()->end(); }
Am validating a form for empty check. All fields works fine. I have a
I am a beginner and I have written a code for validating the form
Validating a form w/ jQuery. For simplicity, user selects from three options: A_or_B, C,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.