9

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);
Bibek
  • 943
  • 1
  • 12
  • 22

3 Answers3

35

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 });
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
2

You can use matches option of express-validator to check if the partner field only contains alphanumeric and has a length of 5

req.checkBody('partnerid', 'Partnerid field must be 5 character long ').matches(/^[a-zA-Z0-9]{5}$/, "i");
sachin.ph
  • 1,078
  • 12
  • 24
0

.len(5) not support in express validation, you can use .isLength(5) to check for a max and min length of 5.

req.checkBody('partnerid', 'Partnerid field must be 5 character long strong text').isLength(5);
swift-lynx
  • 3,219
  • 3
  • 26
  • 45