1

I am trying to validate a username field and I don't want any spaces in the string. I would like to display an error back to the user.

I am using express-validator express middleware to validate the inputs. It works well for every other case but I don't know the best way to validate that there are no spaces.

https://www.npmjs.com/package/express-validator

My code

This is what I have but currently a username with spaces can be stored in the database.

check('user_name').isLength({ min: 1 }).trim().withMessage('User name is required.')

Ideally something I can use with a express-validator method.

Thank you.

gustavohenke
  • 40,997
  • 14
  • 121
  • 129
Daniel
  • 135
  • 3
  • 12
  • Possible duplicate of [Check if a string has white space](https://stackoverflow.com/questions/1731190/check-if-a-string-has-white-space) – ponury-kostek Feb 12 '18 at 14:13

3 Answers3

9

trim is only good for removing spaces around the string, but it doesn't work in the middle of it.

You can write a custom validator easily, though:

check('username')
  .custom(value => !/\s/.test(value))
  .withMessage('No spaces are allowed in the username')

The custom validator uses a regular expression that checks the presence of any whitespace character (could be the usual whitespace, a tab, etc), and negates the result, because the validator needs to return a truthy value to pass.

gustavohenke
  • 40,997
  • 14
  • 121
  • 129
2

Another way of testing for spaces :

console.log(/ /.test("string with spaces")) // true
console.log(/ /.test("string_without_spaces")) // false

And yet another way :

console.log("string with spaces".includes(" ")) // true
console.log("string_without_spaces".includes(" ")) // false
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
1

What happens is: when you use sanitizers in the validation chain, they are only applied during validation time.

If you would like to keep the sanitized value, then you should use the sanitize functions from express-validator/filter:

app.post('/some/path', [
    check('user_name').isLength({ min: 1 }).trim().withMessage('User name is required.'),
    sanitize('user_name').trim()
], function (req, res) {
    // your sanitized user_name here
    let user_name = req.body.user_name
});

If you want to always trim all the request bodies without sanitizing each field, you can use trim-request module, here is an example:

const { check, validationResult } = require('express-validator/check');
const trimRequest = require('trim-request');

app.post('/some/path', trimRequest.body, [
    check('user_name').isLength({ min: 1 }).trim().withMessage('User name is required.'),
], function (req, res) {
    // your sanitized user_name here
    let user_name = req.body.user_name
});
YouneL
  • 8,152
  • 2
  • 28
  • 50
  • "when you use sanitizers in the validation chain, they are only applied during validation time." this was fixed with [v5.0.0 release](https://github.com/ctavan/express-validator/releases/tag/v5.0.0) – gustavohenke Feb 12 '18 at 22:05