A customizable React password validator component with accessibility features
npm install @costilladante/password-validatorbash
npm install @costilladante/password-validator
or
yarn add @costilladante/password-validator
or
pnpm add @costilladante/password-validator
`
> โน You will need to import the styles in your main file (usually at the root of your project, like main.tsx).
`tsx
import { PasswordValidator, defaultRules, FunctionRule } from '@costilladante/password-validator'
import '@costilladante/password-validator/style.css'
const customRule = new FunctionRule(
'customRule', // name of your rule
'Password must be longer than 10 characters', // a message of the requirements
(password: string) => password.length > 10, // the validator function. Can also be a RegEx!
)
const customRuleSet: RuleSet = new RuleSet([...defaultRules.rules, customRule])
function App() {
return (
ruleSet={customRuleSet}
// Optional props
indicators={{
valid: 'โ',
invalid: 'โ',
}}
className="custom-container"
inputClassName="custom-input"
indicatorClassName="custom-indicator"
/>
)
}
`
๐ฎ Examples
$3
Use the pre-built rules by default.
`tsx
`
$3
You can create rules that validate a function or RegEx and show a message.
`tsx
import { RuleSet } from '@costilladante/password-validator'
const myRules: RuleSet = {
rules: [
{
name: 'length',
message: 'At least 8 characters please!',
validate: (password) => (password.length >= 8 ? null : 'Too short!'),
},
{
name: 'uppercase',
message: 'Need at least one BIG letter',
validate: (password) => (/[A-Z]/.test(password) ? null : 'No uppercase found!'),
},
{
name: 'special',
message: 'Add a special character (!@#$%)',
validate: (password) => (/[!@#$%]/.test(password) ? null : 'Missing special character!'),
},
],
}
function App() {
return
}
`
$3
You can easily customize the component.
`tsx
ruleSet={defaultRules}
className="my-container"
inputClassName="my-input"
indicatorsClassName="my-indicators"
indicatorItemClassName="my-indicator-item"
/>
`
๐ ๏ธ Props
| Prop | Type | Required | Default | Description |
| -------------------------- | ------------------------------------------------------ | -------- | ------------------------------ | --------------------- |
| ruleSet | RuleSet | Yes | - | Your password rules |
| indicators | { valid?: React.ReactNode, invalid?: React.ReactNode } | No | { valid: 'โ
', invalid: 'โ' } | Custom checkmarks |
| className | string | No | - | Container class |
| inputClassName | string | No | - | Input field class |
| indicatorsClassName | string | No | - | Rules list class |
| indicatorItemClassName | string | No | - | Individual rule class |
๐จ Styling
The component uses CSS modules, so you can easily override the default styles.
`scss
.my-container {
max-width: 400px;
margin: 20px auto;
}
.my-input {
width: 100%;
padding: 10px;
border: 2px solid #ccc;
border-radius: 4px;
&:focus {
border-color: #007bff;
outline: none;
}
}
.my-indicators {
margin-top: 10px;
padding: 0;
list-style: none;
}
.my-indicator-item {
display: flex;
align-items: center;
gap: 8px;
margin: 5px 0;
color: #666;
&.valid {
color: #4e28a7;
}
&.invalid {
color: #c48322;
}
}
`
๐ป Development
Want to play around with the code? Here's how to get started:
`bash
Clone the repo
git clone https://github.com/yourusername/password-validator.git
cd password-validator
Install dependencies
pnpm install
Start the development server
pnpm dev
Run tests
pnpm test
Run tests in watch mode
pnpm test:watch
Check test coverage
pnpm test:coverage
Build the library
pnpm build
Lint your code
pnpm lint
Lint your styles
pnpm stylelint
Format your code
pnpm format
``