Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}$/)
like image 235
flyingspacecat Avatar asked Oct 20 '25 02:10

flyingspacecat


1 Answers

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}$/)

like image 119
flyingspacecat Avatar answered Oct 21 '25 22:10

flyingspacecat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!