kaizen
kaizen

Reputation: 1638

How to validate regex using express-validator?

I'm trying to validate a mobile number regex using express-validator.

I have browsed through the docs api but couldn't find the validator function that checks a regex. Will using regex.test() like the following work?


body('mobile', 'Invalid mobile number.')
           .escape()
           .exists({checkFalsy: true})
           .isLength({min: 11, max:11})
           .regex.test(/^(\+123)\d{11}$/)

Upvotes: 4

Views: 13815

Answers (2)

Shaheer Ahmed
Shaheer Ahmed

Reputation: 1

If you want to have that matches function in createSchema, you can add like this:

password:{
in:['body'],
errorMessage:"Password required",
isString:true,
matches:{options:{
source:/ain/
}},
withMessage:'Password is not valid'
}

Upvotes: -1

kaizen
kaizen

Reputation: 1638

I found .matches() validator function in express-validator which is the equivalent of regex.test().

body('mobile', 'Invalid mobile number.')
.escape()
.exists({checkFalsy: true})
.isLength({min: 11, max:11})
.matches(/^(09|\+639)\d{9}$/)

Upvotes: 18

Related Questions