Home > front end >  How do I validate number range in NodeJS
How do I validate number range in NodeJS

Time:03-01

When I console.log(req.body.sectionAData), I got this result:

{
  sectionAData: { AQ1: '5', AQ2: '4', AQ3: '5' }
}

So I continue with this:

const { AQ1, AQ2, AQ3} = req.body.sectionAData   
console.log( AQ1, AQ2, AQ3)

and the result is:

5 4 5

What I'm trying to achieve is to validate all the AQ1, AQ2 and AQ3 are within the range of 1 to 5

If either one is not in range, then json.send a message.

How should I go about it? Thanks

CodePudding user response:

You can do like this:

const check = [AQ1, AQ2, AQ3].every(e => e >= 1 && e <= 5)
if (!check) { 
  // do stuff 
}
  • Related