JSFiddle - React, Tailwind, and code Playground

by gabrieleromanato

HTML

<form action="#" method="post" id="form">
    <div>
        <label for="pass">Password:</label>
        <input type="password" name="pass" id="pass" />
        <div id="passstrength"></div>
    </div>
</form>

CSS

#form {
    margin: 2em auto;
    width: 400px;
    font: 90% Arial, sans-serif;
}

#form label {
    display: block;
    font-weight: bold;
    padding-bottom: 5px;
}

#form #pass {
    display: block;
    font: 1.2em Arial, sans-serif;
    padding: 6px 0;
    width: 220px;
    border: 1px solid #aaa;
    border-radius: 5px;
    background: #f9f9f9;
    margin-bottom: 5px;
    box-shadow: inset 1px 1px 1px #ccc;
}

JavaScript

(function($) {


    $.fn.passwordStrength = function(options) {
    
        var that = this;
        
        if(!that.is(':password')) {
        
            throw new Error('Element not supported');
            
            return;
        
        
        }
        
        var settings = {
        
            regexes: {
            
                medium: /^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$/g,
                strong: /^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*$/g
            
            
            },
            
            target: $('#passstrength'),
            
            messages: {
            
                medium: 'Medium',
                strong: 'Strong',
                weak: 'Weak'
            
            
            },
            
            
            styles: {
            
                medium: {color: '#800'},
                strong: {color: '#080'},
                weak: {color: '#c00'}
            
            }
        
        
        };
        
        
        options = $.extend(settings, options);
        
        
        return that.each(function() {
        
            $(this).keyup(function(e) {
            
            
                 if(options.regexes.strong.test($(this).val())) {
                    options.target.html(options.messages.strong).css(options.styles.strong);
                } else if(options.regexes.medium.test($(this).val())) {
                
                    options.target.html(options.messages.medium).css(options.styles.medium);
                
                } else {
                
                    options.target.html(options.messages.weak).css(options.styles.weak);
                
                }
            
                return true;
            
            });
        
        
        
        });
    
    
    
    };


})(jQuery);

$(function() {

    $('#pass').passwordStrength();

});