0.0.15 • Published 4 years ago

simple-password-strength-checker v0.0.15

Weekly downloads
16
License
MIT
Repository
github
Last release
4 years ago

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
0.0.15

4 years ago

0.0.14

4 years ago

0.0.13

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago