Reputation: 470
I am using handlebar page
as template and nodejs
for backend.
I want to use the condtional validation if (a < b)
then show error msg on webpage.
But I am unable to do so with express-validators
.
This is my below code I am trying its not working. I am using 2 letters keyword = 'lt'
indeed but its not working.
router.post('/addtasks', function(req, res, next) {
req.checkBody('topic', 'Empty Topic').notEmpty();
req.checkBody('website', 'Empty Website').notEmpty();
req.checkBody('words', 'Empty Words').notEmpty().isLength({ min: 3 }).isInt({ lt: 2000});
});
The first 2
are working throwing error msg on webpage. But the 3rd one
is not working.
Is there also any other way
or express package
from which I can achieve this?
Thanks!
EDIT FOR WHAT I AM TRYING:
router.post('/addtasks', function(req, res, next) {
req.checkBody('topic', 'Empty Topic').notEmpty();
req.checkBody('website', 'Empty Website').notEmpty();
var totalCount = 2000;
totalCount = totalCount - 500;
req.checkBody('words', 'Empty Words or Min. Words = 500 Required or either Word limit exceeded').notEmpty().isInt({ gt:500, lt: totalCount});
});
Here I want to put the lt:
value dynamically which keeps changing every time not fixed. but here its not taking that. I need a proper solution for this.
Upvotes: 1
Views: 8183
Reputation: 30985
Express-validator
uses the validator.js
library.
Following its documentation
You should use min
and max
properties
req.checkBody('words', 'Empty Words').notEmpty().isInt({ min:100, max: 2000});
Extract :
check if the string is an integer.
options is an object which can contain the keys min and/or max to check the integer is within boundaries (e.g. { min: 10, max: 99 }). options can also contain the key allow_leading_zeroes, which when set to false will disallow integer values with leading zeroes (e.g. { allow_leading_zeroes: false }). Finally, options can contain the keys gt and/or lt which will enforce integers being greater than or less than, respectively, the value provided (e.g. {gt: 1, lt: 4} for a number between 1 and 4).
Upvotes: 7