How can I validate "number of digits" from joi using nodejs? How can I validate "number of digits" from joi using nodejs? mongoose mongoose

How can I validate "number of digits" from joi using nodejs?


Validation for 10 digit numeric mobile number are given below

joi.string().length(10).pattern(/^[0-9]+$/).required(),

You should change the rule as per your requirement. pattern(your regex) is used to accept only digits, length() is used to validate how many character validation accepts and only work with string function.


You can use custom validator

Joi.object({phone: Joi    .string()    .custom((value, helper) => {        // you can use any libs for check phone        if (!checkPhone(value)) {            return helper.message("phone is incorrect")        return value    })}).validate({   phone: '+79002940163'});

Docs:https://github.com/hapijs/joi/blob/master/API.md#anycustommethod-description


Unfortunately, Hapi/joi doesn't presently have a digit count check for numbers. While Sachin Shah is correct that you must convert the number to a string, you must also pass a regex to the validator to ensure that only numeric values are allowed:

joi.string().length(14).regex(/^\d+$/)