There is no way to pass isEmail or isMobilePhone validation with a blank value.
If there is a value of mobile number 2, and I want to check if the entry is a valid mobile number.
I can check that with isMobilePhone validator. But the problem is that the field is not mandatory and if the user does not enter anything, it's currently showing that it is an invalid mobile number.
Same thing with isEmail too.
this is the correct behavior, null is not an email or mobile number. you should handle your form validation yourself this package is to validate strings.
I found a way to solve this with the optional function, from the library itself.
body('mobilenumber2').isMobilePhone().withMessage('Invalid phone number.').optional({
nullable: true
})
It would be great if you could mention the optional function inside the documentation.
This is express-validator a library built on top of validator.js. They are similar but have different aims.
@fedeci thanks for pointing out that for @bimalmithranpb.
For now, I am going to close this issue.
Most helpful comment
This is
express-validatora library built on top ofvalidator.js. They are similar but have different aims.