I need to accept a mobile number as an input for example 97255555555. I need the user to enter first sign and then only digits.
const LoginValidation = (data) => {
const schema = Joi.object({
phone: Joi.string().required().regex("/^([ ]\d*)?$/"),
passwordUser: Joi.string().required(),
});
return schema.validate(data);
};
module.exports.LoginValidation = LoginValidation;
But I'm facing following issue while validating with Joi framework: UnhandledPromiseRejectionWarning: Error: regex must be a RegExp.
CodePudding user response:
let re = RegExp("/^([ ]\d*)?$/";
const LoginValidation = (data) => {
const schema = Joi.object({
phone: Joi.string().required().regex(re),
passwordUser: Joi.string().required(),
});
return schema.validate(data);
};
It's just saying that rather than expecting a regex string. It's expecting a regex object.
(referece) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
CodePudding user response:
I fixed this by changing to pattern instead of regex
const LoginValidation = (data) => {
const schema = Joi.object({
phone: Joi.string().required().pattern(/^([ ]\d*)?$/),
passwordUser: Joi.string().required(),
});
return schema.validate(data);
};
module.exports.LoginValidation = LoginValidation;