Home > Software design >  Validation in NodeJS express
Validation in NodeJS express

Time:11-26

code

if (req.body.phone === undefined || !req.body.phone || req.body.email === undefined || !req.body.email)

how apply validation on this in NodeJS

CodePudding user response:

It depends on the different data that you want to validate and accordingly you can validate.

There is a NPM Validator package that provides a lot of validation properties for different types of data.

You can simply install it and do validation accordingly.

CodePudding user response:

Try this

const phonenumber = function(phoneNo) {
  
  // Basic phone number regex
  const phoneNoRegex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;

  // Returns true if phone number is valid
  // Else returns false
  return phoneNo.match(phoneNoRegex)
  
}

Note: the regex can vary with country codes.

  • Related