2016年9月4日日曜日

開発環境

Exercises for Programmers: 57 Challenges to Develop Your Coding Skills (Brian P. Hogan 著、Pragmatic Bookshelf)のChapter 5(Functions)、25(Password Strength Indicator)を取り組んでみる。

25(Password Strength Indicator)

コード(Emacs)

<p>
  <label for="password0">
    The password
    <input id="password0" type="password" value="12345">
    is a <span id="output0"></span> password.
</p>
<script src="sample25.js"></script>
(function () {
    'use strict';
    var strengths = ['very strong', 'strong', 'weak', 'very weak'],

        input_password = document.querySelector('#password0'),
        span_output = document.querySelector('#output0'),

        passwordValidator,
        output;

    passwordValidator = function (password) {
        var len = password.length;

        if (len >= 8 && /[^a-zA-Z0-9]/.test(password) &&
            /\d/.test(password) && /[a-zA-Z]/.test(password)) {
            return 0;
        }
        if (len >= 8 && /\d/.test(password)) {
            return 1;
        }
        if (len < 8 && /[a-zA-Z]/.test(password)) {
            return 2;
        }
        return 3;
    }
    output = function () {
        var password = input_password.value;

        span_output.innerText = strengths[passwordValidator(password)];
    };

    output();

    input_password.onkeyup = output;
}())

0 コメント:

コメントを投稿

Comments on Google+: