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 8909031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:20:03+00:00 2026-06-15T03:20:03+00:00

I am working on a really basic registration form in Node.js (with Express), and

  • 0

I am working on a really basic registration form in Node.js (with Express), and I am trying to find the easiest way to provide basic form validation. I’ve gone with “Express-Validator”, which seems to do a fine job. However, my goal is to simply show any validation messages that are required and to leave the values entered by the user alone.

It seems that the request information is not making it back into the res.render, which I guess makes sense. However, I’ve looked everywhere I can think of and I can’t find any reference that discusses how to keep form fields populated after showing error messages.

Below is a small snippet describing my approach:

post: function(req, res){

            var userName = req.body.username;
            var password = req.body.password;

            //Validate input
            req.assert("username", 'Invalid email address.').isEmail();
            req.assert("password", 'Password cannot be empty.').notEmpty();
            req.assert("passwordConfirm", 'Passwords entered do not match!').equals(password);

            //Make sure we have no validation errors
            var pageErrors = req.validationErrors();
            if(!pageErrors)
            {
                userModel.CreateUser(userName, password, function(err){
                    if(err)
                    {
                        //there was a problem inserting new user... probably already exists
                        //will need to check the error to confirm
                        var dbErrorMessage = "Could not insert record into database!";
                        if(err.code === 11000)
                        {
                            //this is a duplicate entry
                            dbErrorMessage = "A user with that email address already exists!";
                        }

                        res.render('register.html', { pageErrors: [{msg: dbErrorMessage }]});
                    }
                    else
                    {
                        res.render('register.html', { successMessage: successMessage });
                    }
                });
            }
            else
            {
                res.render('register.html', { pageErrors: pageErrors });
            }
  • 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-15T03:20:04+00:00Added an answer on June 15, 2026 at 3:20 am

    Unfortunately, you have to repopulate the form manually. If you get any page errors, you will pass back the form values to the view.

            if(!pageErrors)
            {
                // ...
            }
            else
            {
                res.render('register.html', { 
                    pageErrors: pageErrors,
                    userName: userName
                });
            }
    

    And in your view, you would do a simple check to see if their are any errors and repopulate accordingly. You would have to keep track of what errors are produced for each form field.

    <% if (userNameError) { %>
        <input type="text" name="userName" value="<%- userName %>" />
    <% } else { %>
        <input type="text" name="userName" />
    <% } %>
    

    Another popular way is to send your form via ajax to to the server, and do all your validations. If there is an error, the entered form data remains and you would show the error, otherwise redirect after the successful login. Below is an example of how to submit a form with javascript.

    $("#login-button").live("submit", function (e) {
    
        // this will prevent the form from being uploaded to the server the conventioanl way
        e.preventDefault();
    
        // the form data
        var data = $(this).serialize();
    
        // this logs the user in 
        $.ajax({
            type: 'POST',
            url: BASE_URL + '/login',
            data: data,
            dataType: 'json',
            success: function (data, status) {
                // successful
            },
    
        });
    
        // superfluous fallback
        return false; 
    }); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with Android and I really need a fast way to get a
Really basic question. Learning Django and now working on development server with nginx set
I am having difficulty with something really basic. I am trying to implement a
I'm trying to do three really basic things inside of a multi-line math mode
i'm looking in a way to make a really basic wysiwyg such as when
G'day All, I am working in R. Sorry about this really basic question, but
I am trying to do something really basic on C but I keep getting
I've been struggling to get a really basic PHP function working on my iMac,
I am trying to do a really basic rename of a file to another
I am working on a countdown timer written in Javascript. Fairly basic really. Just

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.