genie-password-validator
v4.0.3
Published
Customizable strong password validator for Node.js and web applications.
Maintainers
Readme
genie-password-validator
A password validation library for JavaScript
Installation
npm install genie-password-validatorUsage
To use the genie-password-validator package in your project, follow these steps:
- Import the package into your code:
const { isStrongPassword } = require("genie-password-validator");- Define the password to be checked and an optional configuration object:
const password = "YourPassword123!";
const options = {
minUppercase: 1,
minLowercase: 1,
minDigits: 1,
minSpecialChars: 1,
minLength: 8,
};- Call the
isStrongPasswordfunction and pass in the password and options:
const result = isStrongPassword(password, options);- You can then use the
resultobject to handle the validation outcome in your code:
if (result.isValid) {
console.log("Password is strong and meets all criteria.");
} else {
console.log("Password validation failed. Reasons:");
result.messages.forEach(message => {
console.log(message);
});
}Using genie-password-validator in a React.js Project
If you're working on a React.js project and need to implement password validation, you can use the genie-password-validator package as follows:
- Install the package in your React project using npm:
npm install genie-password-validator- Import the
isStrongPasswordfunction in your React component where you need password validation:
import { isStrongPassword } from 'genie-password-validator';- Use the function to validate passwords in your React component, such as during user registration or password change processes. Here's a simple example:
const password = "YourPassword123!";
const options = {
minUppercase: 1,
minLowercase: 1,
minDigits: 1,
minSpecialChars: 1,
minLength: 8,
};
const result = isStrongPassword(password, options);
if (result.isValid) {
console.log("Password is strong and meets all criteria.");
} else {
console.log("Password validation failed. Reasons:");
result.messages.forEach(message => {
console.log(message);
});
}Configuration Options
You can configure the password validation by specifying the following options in the options object:
- minUppercase: Minimum number of uppercase letters.
- minLowercase: Minimum number of lowercase letters.
- minDigits: Minimum number of digits.
- minSpecialChars: Minimum number of special characters (e.g., !@#$%^&*()_+{}[]:;<>,.?~/-).
- minLength: Minimum password length.
License
This package is licensed under the MIT License. Feel free to use and contribute to it.
