Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
express-validator error message attributes and location

I am using expressJS, Node and Handlebars.

In my post function I have the following:

routes.js

req.assert("referenceNumber", "Enter ...").notEmpty();
req.assert("yourPostcode", "Enter ...").notEmpty();

var errors = req.validationErrors(true);

if (errors) {
   res.render('<page>/index', {
   title: "<title>",
   errors: errors
});

} else {
   // Redirection
}

index.hbs

{{#if errors}}
 <ul>
  {{#each errors}}
    <li><a href="#">{{this.msg}}</a></li>
  {{/each}}
 </ul>
{{/if}}

This outputs a list of errors on submission which is great. The problem is I need the error message located next to the form field as well as.

So ideally I can target each error by name e.g. "referenceNumber", "yourPostcode".

I have thought about maybe having a variable inside the routes.js like so:

if (errors) {
   res.render('<page>/index', {
   title: "<title>",
   referenceNumber: <something>,
   yourPostcode: <something>
});

And perhaps be able to do something funky this way.

It would also be very good associating an id with each error message to act as an anchor link to each error form field.

Totally stumped with this one, and would really appreciate some help.

about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!