simple-password-strength-checker
v0.0.15
Published
Validate passwords and check password strengths with custom parameters.
Downloads
44
Maintainers
Readme
Validate passwords and check password strengths with custom parameters.
const config = {
banned: {
words: ['password', 'hello123'],
msg: 'Your password is too common'
},
strict: {
dataTypes: ['string'],
msg: {
string: 'Password must be a string',
int: 'Password can only contain numbers',
alpha: 'Password can only contain letters',
}
},
size: {
min: 8,
max: 24,
msg: {
min: 'Password must be at least 8 character long',
max: 'Password must be at least 24 character long'
}
},
lowercase: {
min: 1,
max: 10,
msg: {
min: 'Password must contain at least 1 lower case characters',
max: 'Password must contain no more than 10 lower case characters',
}
},
uppercase: {
min: 1,
max: 5,
msg: {
min: 'Password must contain at least 1 upper case characters',
max: 'Password must contain no more than 5 upper case characters',
}
},
number: {
min: 1,
max: 4,
msg: {
min: 'Password must contain at least 1 upper case characters',
max: 'Password must contain no more than 5 upper case characters',
}
},
symbol: {
min: 1,
max: 2,
msg: {
min: 'Password must contain at least 1 symbol characters',
max: 'Password must contain no more than 2 symbol characters',
}
}
}
const type = 'validate'
spsc('MyPassword', type, config)
import React, { Component } from "react";
import spsc from "simple-password-strength-checker"
const config = {
size: {
min: 8,
msg: {
min: 'Password must have at least 8 characters'
}
},
uppercase: {
min: 1,
msg: {
min: 'Password must have at least 1 upper case character'
}
},
number: {
min: 1,
msg: {
min: 'Password must have at least 1 number'
}
},
symbol: {
min: 1,
msg: {
min: 'Password must have at least 1 symbol'
}
}
}
class Main extends Component {
constructor(props) {
super(props);
this.state = {
password: '',
color: '',
strength: ''
}
}
passwordCheck(password, type){
var strength = spsc(password, type, config)
if(type === 'validate' && strength.status === 'error'){
alert(strength.msg) // { status: 'error', msg: 'THE ERROR MESSAGE'}
return
}
if(strength <= 0 || password.length === 0){
this.setState({
color: '',
strength: ''
})
} else if(strength <= .5){
this.setState({
color: 'red',
strength: 'Weak password'
})
} else if(strength < 1){
this.setState({
color: 'orange',
strength: 'Average password'
})
} else if(strength === 1){
this.setState({
color: 'green',
strength: 'Strong password'
})
}
}
hC(e, a){
this.setState({
[a]: e.target.value
})
}
render(){
const _ = this.state
return (
<>
<input name="password" type="password" value={_.password} onChange={(e)=>{this.passwordCheck(e.target.value, 'strength'); this.hC(e, 'password')}} />
<div className={_.color}>{_.strength}</div>
<button onClick={()=>{this.passwordCheck(_.password, 'validate')}}>Submit</button>
</>
)
}
}
const spsc = require('simple-password-strength-checker')
var password = 'MySecretPassword'
const config = {
size: {
min: 8,
msg: {
min: 'Password must have at least 8 characters'
}
},
uppercase: {
min: 1,
msg: {
min: 'Password must have at least 1 upper case character'
}
},
number: {
min: 1,
msg: {
min: 'Password must have at least 1 number'
}
},
symbol: {
min: 1,
msg: {
min: 'Password must have at least 1 symbol'
}
}
}
var validate = spsc(password, 'validate', config)
if(validate.status === 'error'){
console.log(validate.msg) // { status: 'error', msg: 'Password must have at least 1 number'}
return
}
var strength = spsc(password, 'strength', config)
console.log(strength) // 0.5