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

9

13

i want that if the number of digits in the input field in less/more than 14(for example) then joi should return an error.

how can i do this with the type of number not for string.

Twine answered 18/9, 2019 at 12:53 Comment(2)
means digit must me equal 14 character ?Reviewer
yes @Saurab MistryTwine
P
19

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.

Polymer answered 7/5, 2020 at 20:56 Comment(1)
There is also \d in js regex. Hence you can also write joi.string().length(10).pattern(/^\d+$/).required(),Gurrola
K
4

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"); 
        } // this is what was added
        return value

    })
}).validate({
   phone: '+79002940163'
});

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

Kootenay answered 11/11, 2020 at 10:29 Comment(0)
D
4

Validation for 10 digit numeric mobile number with custom error message are given below

joi.string().regex(/^[0-9]{10}$/).messages({'string.pattern.base': `Phone number must have 10 digits.`}).required()
Dubonnet answered 10/12, 2020 at 9:37 Comment(0)
I
3

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+$/)

Itinerant answered 17/1, 2020 at 22:54 Comment(0)
P
3
For Mobile number:

mobile: Joi.number().integer().min(10 ** 9).max(10 ** 10 - 1).required().messages({
        'number.min': 'Mobile number should be 10 digit.',
        'number.max': 'Mobile number should be 10 digit'
    })

For other digit check:

yourparam: Joi.number().integer().min(1000).max(9999).required().messages({
        'number.min': 'OTP should be 4 digit.',
        'number.max': 'OTP should be 4 digit'
    })
Paradisiacal answered 5/11, 2022 at 8:20 Comment(1)
If we are trying to store the Number in our MongoDB in Number Type. Then it works. Apart from it above solutions are listed for String Type.Shipping
F
0

For example birth_year an integer between 1900 and 2013

birth_year: Joi.number()
        .integer()
        .min(1900)
        .max(2013)

You can use like this for number with joi. For reference Official Readme

Like this u can pass your min and max values to validate

Franklyn answered 18/9, 2019 at 13:27 Comment(5)
how can we do the same for Aadhaar NumberTwine
do u need to validate aadhar card number is valid r not else just validate the number of digitsFranklyn
in this case just aadhar number but wants to know for others number too.Twine
To check the given number is aadhar number or not, u need to use some third party api's. If need to validate digits alone joi is a good choice.Franklyn
that Joi validation doesn't work for validating number of digitsTwine
C
0

Joi converts strings if it can by default, that's all in the documentation. Globally disable convert or use strict on that specific schema.

Carrissa answered 28/7, 2021 at 12:4 Comment(0)
C
0

minCount: Joi.number().strict()

if you are using @hapi/joi

Curriculum answered 13/8, 2021 at 5:38 Comment(1)
kindly expatiate on your answerPhlogopite
A
-3

You can use it's min and max methods.

joi.number().min(14)
joi.number().max(14)

If it's fine with string then you can do this also.

joi.string().length(14).required()

And while you read the data for db operation, you can use + with value. It will convert your string into integer. But make sure your db allows this big value as a integer if database is mysql. I suggest you to user string as a data type.

let obj = {
    number: +req.body.number
}
Attlee answered 18/9, 2019 at 13:19 Comment(3)
will it take the number which contains 14 digit? for example: 12345678901234Twine
@RaviSingh Yes, and this is not a big deal to try your own. Instead of aking this query, you should try it and I'm sure it will not take much time. :)Attlee
i think i don't want to typecast it to number. or joi don't have any method so that i can really use it instead of writing my own logicsTwine

© 2022 - 2024 — McMap. All rights reserved.