I am trying to validate a string using express validator, specifically with the function isAlphanumeric, but I would like to add space, dash, and single quote as characters to accept.
Here is my code:
body("title")
.trim()
.isLength({ min: 1 })
.escape()
.withMessage("Le champ titre est obligatoire.")
.isAlphanumeric("fr-FR", { ignore: " -'" })
.withMessage("Le champ titre contient des caractères non autorisés."),
With this code, I can type in strings with dashes and spaces in the HTML form without them being rejected, but if I add a single quote, I get a validation error with the last “withMessage” error. The string does not pass the isAlphanumeric test.
I thought of a character escaping problem inside of the “ignore” string, so I have tried the following:
- ” -‘”
- ” -”'” (as seen here on another question) – quote backslash quote quote
- / -‘/ (using RegExp)
- /[ -‘]/ (and every combination with and [], I think)
I cannot find the proper way to include the single quote in the “ignore” option of the isAlphanumeric validator, and I feel like I am trying anything randomly until I find the right way to do it, which is not really satisfying. As everything works fine with other characters, I still think of a character escaping problem but I cannot figure out which one.
I am sure that ignoring single quotes has already been done, but I could not find the answer on Google or Stack Overflow, so I guess it was time I signed up on this site to ask this question by myself.
I would like a “regexp-free” solution if it exists, as it must be doable with the string version (I still need to improve my regexp skill).