How to validate the length of character is equals to 5 using express-validator?

Bibek picture Bibek · May 18, 2018 · Viewed 11.1k times · Source

I am planning to make partnerid field character length as 5 which means user will get error message if he enters less than 5 character or more than 5 characters which will include alpha-numeric character. How can we do it using express-validator? I tried using below code but it didn't worked Thanks

   req.checkBody('partnerid', 'Partnerid field must be 5 character long ').len(5);

Answer

Ankit Agarwal picture Ankit Agarwal · May 18, 2018

You can use isLength() option of the express-validator to check max and min length of 5:

 req.checkBody('partnerid', 'Partnerid field must be 5 character long ').isLength({ min: 5, max:5 });