How to use the matches function from validator

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

validator.matches is a function in Node.js that checks if a string matches a given regular expression.

118
119
120
121
122
123
124
125
126
    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

122
123
124
125
126
127
128
129
130
131
for (key in refs.uids)
{
	key = validator.whitelist(key, 'a-zA-Z0-9');
	if (
		!refs.uids.hasOwnProperty(key)
		|| !(key === 'defaultIcon' || validator.matches(key, /^[A-z0-9]{32}rcp1$/))
	)
	{
		output.error = require('../logs.js')({
			message: 'The refs.json data structure\\\'s ' + key + ' uid is not a valid game uid.',
fork icon5
star icon20
watch icon0

How does validator.matches work?

validator.matches is a function in the validator library that checks whether a string matches a regular expression pattern. It takes two arguments: the string to be checked and the regular expression pattern to match against, and returns a boolean value indicating whether there is a match or not.

210
211
212
213
214
215
216
217
218
219
    return this;
  }
  // console.log(key)
  // console.log(_flatCurArgs[key])
  // console.log(_flatCompArgs[key])
  this.flag = validator.matches(_flatCurArgs[key], _flatCompArgs[key]);
} else {
  this.flag = false;
  return this;
}
fork icon0
star icon1
watch icon9

+ 19 other calls in file

130
131
132
133
134
135
136
137
138
139
// half-width chars.
isVariableWidth: validator.isVariableWidth,
// matches(str, pattern [, modifiers]) - check if string matches the pattern.
// Either matches('foo', /foo/i) or matches('foo', 'foo', 'i').
matches: function (str, options) {
  return validator.matches(str, options.pattern, options.modifiers);
},
// Allow custom validators
validate: function (str, fn) {
  return fn(str);
fork icon0
star icon1
watch icon3

+ 17 other calls in file

Ai Example

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

const pattern = /^[a-zA-Z\s]+$/; // pattern to match alphabetic characters and whitespace

const str1 = "Hello World";
const str2 = "12345";

console.log(validator.matches(str1, pattern)); // true
console.log(validator.matches(str2, pattern)); // false

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


const validate = (params) => {
    let name = !validator.isEmpty(params.name) &&
        validator.isLength(params.name, { min: 3, max: undefined }) &&
        validator.matches(params.name, /^[a-zA-Z\s]+$/)




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

+ 2 other calls in file

17
18
19
20
21
22
23
24
25
26
* @return {String} - A text in json informing of the BDD input of a new order.
*/
creatOrder: async (req, res) => {
    try {
        if (validator.equals(typeof req.body.infoOrder.name, 'string')
            && validator.matches(req.body.infoOrder.phone, /^(?:(?:\+|00)33|0)\s*[1-9](?:[\s.-]*\d{2}){4}$/)
            && validator.toDate(req.body.infoOrder.orderDate)
            && validator.equals(typeof req.body.infoOrder.totalPrice, 'number')
            && validator.equals(typeof req.body.userInfo, 'number')) {
            const newOrder = new OrderModel({
fork icon0
star icon0
watch icon1

131
132
133
134
135
136
137
138
139
140
    .custom((value, {
    req
}) => {
    // validate username if it is a privileged user
    // username from unprivileged users will be ignored
    if ((req.user.priv != 0) || validator.matches(value, /^[a-zA-Z0-9]{3,128}$/)) {
        return true;
    }
    req.body.username = "";
    return false;
fork icon0
star icon0
watch icon0

11
12
13
14
15
16
17
18
19
20
link: {
  type: String,
  required: true,
  validate: {
    validator(v) {
      return validator.matches(v, patternurl);
    },
    message: 'Поле "link" должно быть ссылкой',
  },
},
fork icon0
star icon0
watch icon0

72
73
74
75
76
77
78
79
80
81
82
83
validator.extend('isEmptyOrURL', function isEmptyOrURL(str) {
    return (_.isEmpty(str) || validator.isURL(str, {require_protocol: false}));
});


validator.extend('isSlug', function isSlug(str) {
    return validator.matches(str, /^[a-z0-9\-_]+$/);
});


/**
 * Validation against simple password rules
fork icon0
star icon0
watch icon0

+ 2 other calls in file

96
97
98
99
100
101
102
103
104
105

if (req.body.department) {
    isDepartment = validator.matches(String(department), regExText);
}
if (req.body.role) {
    isRole = validator.matches(String(role), regExText);
}
if (req.body.linkedin_url) {
    isLinkedinUrl = validator.isURL(String(linkedin_url), [["http", "https"]]);
}
fork icon0
star icon0
watch icon0

+ 3 other calls in file

38
39
40
41
42
43
44
45
46
const { firstName, lastName, email, password } = req.body;

// RegEx Text
const regExText = /^[A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ \'\- ]+$/i;
// Validation donnés de l'utilisateur
let isFirstName = validator.matches(String(firstName), regExText);
let isLastName = validator.matches(String(lastName), regExText);
let isEmail = validator.isEmail(String(email));
let isPassword = passValid.validate(String(password), options).valid;
fork icon0
star icon0
watch icon0