Nodejs Validation

By ukmodak | March 31st 2024 10:31:18 AM | viewed 931 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
Visitor

Total : 26654

Today :3

Today Visit Country :

  • France
  • United States