How to use the contains function from validator

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

validator.contains is a function that checks if a string contains a specified substring, and returns a boolean value.

22
23
24
25
26
27
28
29
30
31
},
equals: function(rule, value) {
  return validator.equals(value, rule);
},
contains: function(rule, value) {
  return validator.contains(value, rule);
},
isEmail: function(rule, value) {
  if (rule) {
    return validator.isEmail(value);
fork icon0
star icon2
watch icon2

+ 5 other calls in file

165
166
167
168
169
170
171
172
173
174
                this.addError(tip || this.key + " is must contain " + s + ".");
        }
        return this;
};
Validator.prototype.notContains = function(s, tip) {
        if (this.goOn && v.contains(this.value,s)) {
                this.addError(tip || this.key + " is must not contain " + s + ".");
        }
        return this;
};
fork icon34
star icon0
watch icon2

+ 5 other calls in file

How does validator.contains work?

validator.contains is a function provided by the validator library that checks whether a string contains a specified substring, and returns a boolean value indicating the result. It takes two arguments, the first being the input string to search in, and the second being the substring to search for. The function uses the indexOf() method to perform the search and returns true if the substring is found, and false otherwise. The search is case-sensitive by default, but can be made case-insensitive by setting the third argument to true.

1
2
3
4
5
6
7
8
9
10

function StringStrategy() {
    this.name = "String";

    this.contains = (seed) => (subject) => {
        return StringValidator.contains(subject, seed) ? null : 'Does not contain ' + seed;
    }

    this.equals = (comparison) => (subject) => {
        return StringValidator.equals(subject, comparison) ? null : 'Does not equal ' + comparison;
fork icon1
star icon4
watch icon2

+ 3 other calls in file

161
162
163
164
165
166
167
168
169
170
// Validating the request for otp
  if (
    typeof req.body.otp !== "string" ||
    validator.isEmpty(req.body.otp) ||
    validator.isWhitelisted(req.body.otp, " ") || 
    validator.contains(req.body.otp, ' ') ||
    !validator.isLength(req.body.otp, { min:4, max:4 })
  ) {
    return responseGenerator(res,400,false,"Valid OTP is required in String format and must not be Empty. Check Your Email for a 4 Digit OTP valid for 5 minutes",[]);
  }
fork icon0
star icon0
watch icon1

+ 8 other calls in file

Ai Example

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

const str = "Hello, world!";
const subStr = "world";

if (validator.contains(str, subStr)) {
  console.log(`The string "${str}" contains the substring "${subStr}".`);
} else {
  console.log(
    `The string "${str}" does not contain the substring "${subStr}".`
  );
}

Output: c Copy code