Nodejs Validation

By ukmodak | April 29th 2021 06:09:08 AM | viewed 154 times
D:\nodejs_mongodb_project>npm install --save express-validator

Add the following code in the controller

const { check, validationResult } = require('express-validation')
exports.admin_regi_add = function (req, res) {

    check('fullname').isLength({ min: 3 }),
        check('fullname').isLength({ min: 3 }),
        check('mobile').isLength({ min: 3 }),
        check('pword').isLength({ min: 3 }),
        check('cpword').isLength({ min: 3 }),
        check('birth').isLength({ min: 3 }),
        check('sex').isLength({ min: 3 }),
        check('prefer_con').isLength({ min: 3 }),
        check('profile_pic').isLength({ min: 3 }),
        check('email').isEmail()

    const errors = validationResult(req)
    if (!errors.isEmpty()) {
        return res.status(422).json({ errors: errors.array() })
    }

};

These are validation type

  • isLength()
  • isEmail()
  • isNumeric()
  • contains(), check if value contains the specified value
  • equals(), check if value equals the specified value
  • isAlpha()
  • isAlphanumeric()
  • isAscii()
  • isBase64()
  • isBoolean()
  • isCurrency()
  • isDecimal()
  • isEmpty()
  • isFQDN(), is a fully qualified domain name?
  • isFloat()
  • isHash()
  • isHexColor()
  • isIP()
  • isIn(), check if the value is in an array of allowed values
  • isInt()
  • isJSON()
  • isLatLong()
  • isLength()
  • isLowercase()
  • isMobilePhone()
  • isNumeric()
  • isPostalCode()
  • isURL()
  • isUppercase()
  • isWhitelisted(), checks the input against a whitelist of allowed characters
  • isAfter(), check if the entered date is after the one you pass
  • isBefore(), check if the entered date is before the one you pass
  • isISO8601()
  • isRFC3339()
bONEandALL