With JavaScript on the front end, I created regex that allows letters, numbers and some special characters like so....
function onlyAlphaSomeChar(value) {
const re = /^[A-Za-z0-9 .,'!&]+$/;
return re.test(value);
}
what would be the equivalent of this if I were to build the validation process with express-validator on the backend?
I have this much created within my ExpressJs environment, but not sure what next steps should look like...
//...
app.post('/', [
//VALIDATE
check('comment')
.notEmpty()
.withMessage('Comment required')
.isLength({min: 3,max:280})
.withMessage('Comment must be between 3 and 280 characters')
.escape()
], (req, res) => {
//...
});
To check against a regular expression, you can use .match(regexp)
.
Thus, here, you could do:
//...
app.post('/', [
//VALIDATE
check('comment')
.escape()
.notEmpty()
.withMessage('Comment required')
.isLength({min: 3,max:280})
.withMessage('Comment must be between 3 and 280 characters')
.matches(/^[A-Za-z0-9 .,'!&]+$/)
], (req, res) => {
//...
});
```
Does this answer your question?