Search code examples
javascriptnode.jsjoi

Node js Joi validation to send error if string contains space at start or end


How to create JOI validation with Node js if string contains empty space at start or end send error

example : if input is
name = "test123" //valid
name = "test(space)" or "(space)test" // invalid


Solution

  • You can use below regex to validate the your cases

    Joi.string().pattern(new RegExp('^([a-zA-Z0-9]+( [a-zA-Z0-9]+)*)$', 'g'))