I'm trying to give error if the input field i.e name not only consists of alphabets in express-validator
req.check('name')
.isLength({min:3}).withMessage('Name must be of 3 characters long.')
.isAlpha().withMessage('Name must be alphabetic.');
but when i enter "John Doe" in "name" input field it says "Name must be alphabetic" instead of successful validation
.isAlpha()
method description from validator.js documentation (express-validator is also a wrapper for validation functions of this module):
check if the string contains only letters (a-zA-Z)
Your string John Doe
contains a whitespace, that's why validation is not succesfull.
Your validation chain can be this one:
req.check('name')
.isLength({min:3}).withMessage('Name must be of 3 characters long.')
.matches(/^[A-Za-z\s]+$/).withMessage('Name must be alphabetic.')
.isAlpha()
is replaced with matches()
. Validation is successful when name
is a string with 3 characters and more (alphabetic characters or whitespaces only).
Source: validator.js validators