Générateur de mot de passe

Démo




Source

<style>
    #demo {
        background: #eee;
        padding: .5rem;
        border-radius: 0.25rem;
        text-align: center;
    }

    input[type=text] {
        padding: .2rem;
        border: 1px solid #a5a4a4;
        border-radius: 0.25rem;
        height: 1.5rem;
    }

    input[type=button], input[type=submit], input[type=reset] {
        border: none;
        color: #FFF;
        background: #963c7b;
        margin: .5rem 0;
        padding: .5rem;
        cursor: pointer;
        text-decoration: none;
        color: #FFF;
        border-radius: 0.25rem;
    }
</style>
<div id="demo">
    <input type="checkbox" id="min" checked="checked" />
    <label for='min'>abc</label>

    <input type="checkbox" id="maj" checked="checked" />
    <label for='maj'>ABC</label>

    <input type="checkbox" id="chi" checked="checked" />
    <label for='chi'>234</label>

    <input type="checkbox" id="aut" />
    <label for='aut'>Autre</label>

    <input type="checkbox" id="io0" />
    <label for='io0'>iol</label>
    <br/>
    <label>Nombre de caractères</label>
    <input type="text" id="nb" size="2" value="12" class="text-center" />
    <br/>
    <input type="button" value="Générer" onclick="gen()" />
    <br/>
    <input type="text" id="res" class="text-center" />
</div>
<script>
    function gen() {
        var cMin = "abcdefghjkmnpqrstuvwxyz";
        var cMaj = "ABCDEFGHJKMNPQRSTUVWXYZ";
        var cChi = "23456789";
        var cAut = ",?;.:/!§*µù%$£&é(-è_çà)=+";
        var cIo0 = "iol10IOL";

        var fMin = fMaj = fChi = fAut = fIo0 = false;

        var pass = "";

        if (document.getElementById("min").checked)
            fMin = true;
        if (document.getElementById("maj").checked)
            fMaj = true;
        if (document.getElementById("chi").checked)
            fChi = true;
        if (document.getElementById("aut").checked)
            fAut = true;
        if (document.getElementById("io0").checked)
            fIo0 = true;

        if (!fMin && !fMaj && !fChi && !fAut && !fIo0) {
            document.getElementById("res").value = "Cocher au moins une case !";
        } else {
            var nb = parseInt(document.getElementById("nb").value);
            if (!isNaN(nb)) {
                i = 0;
                while (i < nb) {
                    if (i < nb && fMin) {
                        pass += cMin.charAt(intRand(cMin.length - 1));
                        i++
                    }
                    if (i < nb && fMaj) {
                        pass += cMaj.charAt(intRand(cMaj.length - 1));
                        i++
                    }
                    if (i < nb && fChi) {
                        pass += cChi.charAt(intRand(cChi.length - 1));
                        i++
                    }
                    if (i < nb && fAut) {
                        pass += cAut.charAt(intRand(cAut.length - 1));
                        i++
                    }
                    if (i < nb && fIo0) {
                        pass += cIo0.charAt(intRand(cIo0.length - 1));
                        i++
                    }
                }
                pass = pass.split("");
                pass.sort(function() {
                    return 0.5 - Math.random()
                });
                pass = pass.join("");
                document.getElementById("res").value = pass;
                document.getElementById("res").focus();
                document.getElementById("res").select();
            } else {
                document.getElementById("res").value = "Nombre de caractères incorrect !";
            }
        }
    }

    function intRand(max) {
        return Math.round(Math.random() * max);
    }
</script>