How to use the isStrongPassword function from validator

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

validator.isStrongPassword is a function provided by the validator library that checks whether a given string meets strong password criteria.

16
17
18
19
20
21
22
23
24
25
password: {
  type: String,
  required: [true, "Password is required"],
  validate: {
    validator: (value) =>
      validator.isStrongPassword(value, {
        minLength: 6,
        minLowercase: 3,
        minNumbers: 1,
        minUppercase: 1,
fork icon1
star icon1
watch icon1

9
10
11
12
13
14
15
16
17
18
async register(req, res, next) {
    const data = req.body;
    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")
    ) {
fork icon1
star icon0
watch icon0

How does validator.isStrongPassword work?

validator.isStrongPassword is a function provided by the validator library that checks whether a given string meets strong password criteria. The function takes in two arguments: the string to test and an options object that specifies the criteria for a strong password. By default, isStrongPassword checks whether the string contains at least one uppercase letter, one lowercase letter, one number, and one special character, and whether the length of the string is at least 8 characters. The options object can be used to customize these criteria by specifying the following properties: minLength - the minimum length of the string maxLength - the maximum length of the string minLowercase - the minimum number of lowercase letters minUppercase - the minimum number of uppercase letters minNumbers - the minimum number of digits minSymbols - the minimum number of special characters When called, isStrongPassword checks whether the string meets the specified criteria, and returns true if it does and false if it does not. Overall, validator.isStrongPassword provides a simple way to check whether a given string meets strong password criteria, which can be useful for validating passwords in a web application.

8
9
10
11
12
13
14
15
16
17
const message="Welcome as an Amazon member"
//SIGN UP 
exports.signUp = (req, res, next) => {
    console.log(req.body)
    let validEmail = validator.isEmail(req.body.email);
    let validPass = validator.isStrongPassword(req.body.password);
    let validPhone = validator.isMobilePhone(req.body.phone, ['ar-EG']);
    // if (validEmail && validPass && validPhone) {
        // console.log(req.body)
        User.findOne({ email: req.body.email })
fork icon0
star icon0
watch icon1

110
111
112
113
114
115
116
117
118
119
validator: function (value) {
  if (this.role === "admin") {
    // skip password validation for admin users
    return true;
  }
  return validator.isStrongPassword(value, {
    minLength: 6,
    minLowercase: 3,
    minNumbers: 1,
    minUppercase: 1,
fork icon0
star icon0
watch icon1

Ai Example

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

const password1 = "weakpassword";
const password2 = "Strongpassword123!";

const options = {
  minLength: 8,
  minLowercase: 1,
  minUppercase: 1,
  minNumbers: 1,
  minSymbols: 1,
};

console.log(validator.isStrongPassword(password1)); // false
console.log(validator.isStrongPassword(password2, options)); // true

In this example, we use validator.isStrongPassword to check whether two strings meet strong password criteria. We first import the validator library using the require function and assign it to the variable validator. We create two passwords, password1 and password2. We also create an options object that specifies the criteria for a strong password, including a minimum length of 8 characters and minimum requirements for lowercase letters, uppercase letters, numbers, and special characters. We call validator.isStrongPassword twice, passing in password1 and password2 as the first argument, and options as the second argument for the second call. The first call returns false because password1 does not meet the specified criteria for a strong password. The second call returns true because password2 meets the specified criteria for a strong password. Note that in order to use validator.isStrongPassword, you need to have the validator library installed and imported in your application.

149
150
151
152
153
154
155
156
157
158
const oldUser = new User(user.gender, user.name, user.firstname, user.email, user.password, user.date_cookie, new Date().toISOString().slice(0, 10), user.admin);
if (!bcrypt.compareSync(oldPassword, oldUser.password)) {
  res.status(401).send({ message: `Authentication failed, please verify your information.` });
  return;
}
if (newPassword && validator.isStrongPassword(newPassword) && newPassword !== newPasswordConfirm) {
  res.status(401).send({ message: 'Confirmation of new password is failed' });
  return;
}
oldUser.id = escape(user.id);
fork icon0
star icon0
watch icon1

13
14
15
16
17
18
19
20
21
password: {
  type: String,
  // required: [true, "Password is required"],
  // validate: {
  //   validator: (value) =>
  //     validator.isStrongPassword(value, {
  //       minLength: 6,
  //       minLowercase: 3,
  //       minNumbers: 1,
fork icon0
star icon0
watch icon1

13
14
15
16
17
18
19
20
21
22
password:{
    type:String,
    required:[true,"Please provide password"],
    validate:{
        validator:(value)=>{
            validator.isStrongPassword(value,{
                minLength:6,
                minLowercase:3,
                minNumber:1,
                nimUppercase:1,
fork icon0
star icon0
watch icon1

+ 8 other calls in file

4
5
6
7
8
9
10
11
12
13
14
15
    return validator.isEmail(email);
}


// Validate password
exports.validatePassword = (password) => {
    return validator.isStrongPassword(password);
}


// Validate name

fork icon0
star icon0
watch icon0

6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
function validateEmail(email) {
  return validator.isEmail(email)
}


function validatePassword(password) {
  return validator.isStrongPassword(password, {
    minLength: 8,
    minLowercase: 1,
    minUppercase: 1,
    minNumbers: 1,
fork icon0
star icon0
watch icon0

25
26
27
28
29
30
31
32
33
34
35
}


console.log(validator.ltrim(password));
console.log(validator.isEmpty(password));
console.log(
  validator.isStrongPassword(password, {
    minLength: 8,
    minLowercase: 1,
    minUppercase: 1,
    minSymbols: 1,
fork icon0
star icon0
watch icon0