How to use the isLength function from validator

Find comprehensive JavaScript validator.isLength code examples handpicked from public code repositorys.

The validator.isLength function checks if a given string's length is within a specified range.

39
40
41
42
43
44
45
46
47
48
exports.isEmail = function (value) {
  return validator.isEmail(value)
}

exports.isLessThanLength = function (value, length) {
  return validator.isLength(value, { max: length })
}

exports.isInteger = function (value) {
  return validator.isInt(value)
fork icon2
star icon5
watch icon35

214
215
216
217
218
219
220
221
    return validator.isMongoId(value);
  }
  return true;
},
isLength: function(rule, value) {
  return validator.isLength(value, rule[0], rule[1]);
},
matches: function(rule, value) {
fork icon0
star icon2
watch icon2

+ 5 other calls in file

How does validator.isLength work?

The validator.isLength function is a part of the validator.js library and is used to check if a given string's length is within a specified range. To accomplish this, the function accepts two arguments: str and options. The str argument is the string to be checked. The options argument is an object that can contain up to three properties: min, max, and locale. The min property specifies the minimum length that the string must be in order to pass the validation check. If min is not specified, the function does not check for a minimum length. The max property specifies the maximum length that the string can be in order to pass the validation check. If max is not specified, the function does not check for a maximum length. The locale property specifies the locale to be used when counting the string length. If locale is not specified, the default locale is used. If the length of the given string falls within the specified range (as determined by the min and max properties), the function returns true. Otherwise, it returns false. By using the validator.isLength function, developers can programmatically check if a given string's length is within a specified range, which can be useful in scenarios where they need to validate user input or ensure that string lengths meet certain requirements.

17
18
19
20
21
22
23
24
25
26
  return (
    validator.isAlphanumeric(username) &&
    /^((84|0[3|5|7|8|9])+([0-9]{8}))$/.test(mobilephone) &&
    validator.isIn(usertype, [ 1, 2]) &&
    validator.isLength(username, { min: 4, max: 32 }) &&
    validator.isLength(password, { min: 6, max: 32 })
  );
}

async signup(req, res) {
fork icon0
star icon1
watch icon0

92
93
94
95
96
97
98
99
100
101
// isJSON(str) - check if the string is valid JSON (note: uses JSON.parse).
isJSON: validator.isJSON,
// isLength(str, min [, max]) - check if the string's length falls in a range.
// Note: this function takes into account surrogate pairs.
isLength: function (str, options) {
  return validator.isLength(str, options.min, options.max);
},
// isLowercase(str) - check if the string is lowercase.
isLowercase: validator.isLowercase,
// isMobilePhone(str, locale) - check if the string is a mobile phone number,
fork icon0
star icon1
watch icon3

+ 17 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const validator = require("validator");

const input1 = "Hello, world!";
const input2 = "Hello";

if (validator.isLength(input1, { min: 10, max: 20 })) {
  console.log("Input 1 meets the length requirements.");
} else {
  console.log("Input 1 does not meet the length requirements.");
}

if (validator.isLength(input2, { min: 10, max: 20 })) {
  console.log("Input 2 meets the length requirements.");
} else {
  console.log("Input 2 does not meet the length requirements.");
}

In this example, we're using the validator.isLength function to check if two input strings meet certain length requirements. We define two input strings, input1 and input2, and use the isLength function to check if they have a length between 10 and 20 characters. We use the min and max properties of the options argument to specify the length requirements. If a given input string meets the length requirements, we log a message indicating that it meets the requirements. Otherwise, we log a message indicating that it does not meet the requirements. When we run this code, it will output messages indicating whether or not each input string meets the length requirements. This demonstrates how validator.isLength can be used to programmatically check if a given string's length is within a specified range in JavaScript code.

115
116
117
118
119
120
121
122
123
124
},
'lessThan' : function (x, val) {
  var number = parseFloat(x);
  return isNaN(number) || number < val;
},
      'minLength'     : function (x, min) { return validator.isLength(x, min); },
      'maxLength'     : function (x, max) { return validator.isLength(x, 0, max); },

      'regex' : function (x, regex) { return validator.matches(x, regex); },
      'notRegex' : function (x, regex) { return !validator.matches(x, regex); }
fork icon59
star icon0
watch icon7

+ 3 other calls in file

11
12
13
14
15
16
17
18
19
20
if (
    validator.isAlphanumeric(data.userName) &&
    validator.isLength(data.userName, { min: 3, max: 16 }) &&
    validator.isStrongPassword(data.password) &&
    validator.isAlphanumeric(data.name) &&
    validator.isLength(data.name, { min: 3, max: 16 }) &&
    validator.isMobilePhone(data.phone, "vi-VN")
) {
    await Customer.findOne({ userName: data.userName })
        .then(async (user) => {
fork icon1
star icon0
watch icon0

+ 4 other calls in file

-1
fork icon0
star icon0
watch icon1

+ 7 other calls in file

-1
fork icon0
star icon0
watch icon1

+ 2 other calls in file

0
1
2
3
4
5
6
7
8
9
10
11
const validator = require("validator");


const validarArticulo = (parametros) => {
    
    let validar_titulo = !validator.isEmpty(parametros.titulo) && 
                            validator.isLength(parametros.titulo, {min: 5, max: undefined});
    let validar_contenido = !validator.isEmpty(parametros.contenido);


    if(!validar_titulo || ! validar_contenido){
        throw new Error("No se ha validado la información !!");
fork icon0
star icon0
watch icon1

+ 9 other calls in file

13
14
15
16
17
18
19
20
21
22
const { email, password, role } = req.body;

if (
  !(
    validator.isEmail(email) &&
    validator.isLength(password, { min: 8, max: undefined })
  )
) {
  return res
    .status(400)
fork icon0
star icon0
watch icon1

11
12
13
14
15
16
17
18
19
20
// Collect the data to save
let parameters = req.body;

// Validate data
try {
  let validateTittle = !validator.isEmpty(parameters.tittle) && validator.isLength(parameters.tittle, { min: 5, max: undefined });
  let validateContent = !validator.isEmpty(parameters.content);

  if (!validateTittle || !validateContent) {
    throw new Error("The information has not been validated");
fork icon0
star icon0
watch icon1

24
25
26
27
28
29
30
31
32
33

// Validar datos
try{

    let validar_titulo = !validator.isEmpty(parametros.titulo) 
        && validator.isLength(parametros.titulo, {min:5, max:undefined})
    let validar_contenido = !validator.isEmpty(parametros.contenido)
    
    if(!validar_contenido || !validar_titulo){
        throw new Error("No se ha validado la informacion !!");
fork icon0
star icon0
watch icon1

+ 7 other calls in file

4
5
6
7
8
9
10
11
12
let name = !validator.isEmpty(params.name) &&
    validator.isLength(params.name, { min: 3, max: undefined }) &&
    validator.isAlpha(params.name, "es-ES")

let nick = !validator.isEmpty(params.nick) &&
    validator.isLength(params.nick, { min: 2, max: 60 })

let email = !validator.isEmpty(params.email) &&
    validator.isEmail(params.email)
fork icon0
star icon0
watch icon1

+ 7 other calls in file

1
2
3
4
5
6
7
8
9
10
11
12


//Validate data
const validateArticle = (parameters) => {
  let validateTitle =
    !validator.isEmpty(parameters.title) &&
    validator.isLength(parameters.title, { min: 5, max: undefined });
  let validateContent = !validator.isEmpty(parameters.content);


  if (!validateTitle || !validateContent) {
    throw new Error("The information has not been validated");
fork icon0
star icon0
watch icon1

19
20
21
22
23
24
25
26
27
28
//     {
//       validator: (value) => !isEmpty(value),
//       message: 'Описание программы не может быть пустым',
//     },
//     {
//       validator: (value) => isLength(value, { min: 30, max: undefined }),
//       message: 'Описание программы должно быть длиннее 30 символов',
//     },
//   ]);
// }
fork icon0
star icon0
watch icon1

21
22
23
24
25
26
27
28
29
30

// Validate data
try {

    let validate_title = !validator.isEmpty(params.title) && 
    validator.isLength(params.title, {min: 5, max: undefined});
    let validate_content = !validator.isEmpty(params.content);

    if(!validate_title || ! validate_content) {
        throw new Error('The information has not been validated!');
fork icon0
star icon0
watch icon1

+ 7 other calls in file

-1
fork icon0
star icon0
watch icon1

+ 7 other calls in file