Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Firstly include jQuery and the strength.css and strength.js files. Place these b
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="strength.js"></script>
<script type="text/javascript" src="js.js"></script>


##### Create a password input field

Expand All @@ -37,23 +37,30 @@ Once you have created your password input field you will need to initiate the pl

At its most basic level you can initiate the plugin like:


$(document).ready(function ($) {

$("#myPassword").strength();

});


If you want to initiate the plugin with options then you can do so like:


$('#myPassword').strength({
strengthClass: 'strength',
strengthMeterClass: 'strength_meter',
strengthButtonClass: 'button_strength',
strengthButtonText: 'Show password',
strengthButtonTextToggle: 'Hide Password'
strengthButtonTextToggle: 'Hide Password',
strengthText: {
veryweak : 'Very week',
week: 'Week',
medium: 'Medium',
strong: 'Strong',
strength: 'Strength'
}
});

#### ..:: Options
Expand Down
81 changes: 44 additions & 37 deletions src/strength.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@
strengthMeterClass: 'strength_meter',
strengthButtonClass: 'button_strength',
strengthButtonText: 'Show Password',
strengthButtonTextToggle: 'Hide Password'
strengthButtonTextToggle: 'Hide Password',
strengthText: {
veryweak : 'Very week',
week: 'Week',
medium: 'Medium',
strong: 'Strong',
strength: 'Strength'
}

};

// $('<style>body { background-color: red; color: white; }</style>').appendTo('head');
// $('<style>body { background-color: red; color: white; }</style>').appendTo('head');

function Plugin( element, options ) {
this.element = element;
Expand All @@ -37,48 +45,49 @@
var number = 0;
var special = 0;

var upperCase= new RegExp('[A-Z]');
var lowerCase= new RegExp('[a-z]');
var upperCase = new RegExp('[A-Z]');
var lowerCase = new RegExp('[a-z]');
var numbers = new RegExp('[0-9]');
var specialchars = new RegExp('([!,%,&,@,#,$,^,*,?,_,~])');
var strengthText = this.options.strengthText;

function GetPercentage(a, b) {
return ((b / a) * 100);
}
return ((b / a) * 100);
}

function check_strength(thisval,thisid){
if (thisval.length > 8) { characters = 1; } else { characters = -1; };
if (thisval.match(upperCase)) { capitalletters = 1} else { capitalletters = 0; };
if (thisval.match(lowerCase)) { loweletters = 1} else { loweletters = 0; };
if (thisval.match(numbers)) { number = 1} else { number = 0; };
function check_strength(thisval,thisid){
if (thisval.length > 8) { characters = 1; } else { characters = -1; };
if (thisval.match(upperCase)) { capitalletters = 1} else { capitalletters = 0; };
if (thisval.match(lowerCase)) { loweletters = 1} else { loweletters = 0; };
if (thisval.match(numbers)) { number = 1} else { number = 0; };

var total = characters + capitalletters + loweletters + number + special;
var totalpercent = GetPercentage(7, total).toFixed(0);
var total = characters + capitalletters + loweletters + number + special;
var totalpercent = GetPercentage(7, total).toFixed(0);

if (!thisval.length) {total = -1;}
if (!thisval.length) {total = -1;}

get_total(total,thisid);
}
get_total(total,thisid);
}

function get_total(total,thisid){

var thismeter = $('div[data-meter="'+thisid+'"]');
if (total <= 1) {
thismeter.removeClass();
thismeter.addClass('veryweak').html('very weak');
if (total <= 1) {
thismeter.removeClass();
thismeter.addClass('veryweak').html(strengthText.veryweak);
} else if (total == 2){
thismeter.removeClass();
thismeter.addClass('weak').html('weak');
thismeter.addClass('weak').html(strengthText.week);
} else if(total == 3){
thismeter.removeClass();
thismeter.addClass('medium').html('medium');
thismeter.addClass('medium').html(strengthText.medium);

} else {
thismeter.removeClass();
thismeter.addClass('strong').html('strong');
thismeter.removeClass();
thismeter.addClass('strong').html(strengthText.strong);
}
if (total == -1) { thismeter.removeClass().html('Strength'); }

if (total == -1) { thismeter.removeClass().html(strengthText.strength); }
}


Expand All @@ -92,29 +101,29 @@

thisid = this.$elem.attr('id');

this.$elem.addClass(this.options.strengthClass).attr('data-password',thisid).after('<input style="display:none" class="'+this.options.strengthClass+'" data-password="'+thisid+'" type="text" name="" value=""><a data-password-button="'+thisid+'" href="" class="'+this.options.strengthButtonClass+'">'+this.options.strengthButtonText+'</a><div class="'+this.options.strengthMeterClass+'"><div data-meter="'+thisid+'">Strength</div></div>');
this.$elem.addClass(this.options.strengthClass).attr('data-password',thisid).after('<input style="display:none" class="'+this.options.strengthClass+'" data-password="'+thisid+'" type="text" name="" value=""><a data-password-button="'+thisid+'" href="" class="'+this.options.strengthButtonClass+'">'+this.options.strengthButtonText+'</a><div class="'+this.options.strengthMeterClass+'"><div data-meter="'+thisid+'">'+strengthText.strength+'</div></div>');

this.$elem.bind('keyup keydown', function(event) {
thisval = $('#'+thisid).val();
$('input[type="text"][data-password="'+thisid+'"]').val(thisval);
check_strength(thisval,thisid);

});

$('input[type="text"][data-password="'+thisid+'"]').bind('keyup keydown', function(event) {
$('input[type="text"][data-password="'+thisid+'"]').bind('keyup keydown', function(event) {
thisval = $('input[type="text"][data-password="'+thisid+'"]').val();
console.log(thisval);
$('input[type="password"][data-password="'+thisid+'"]').val(thisval);
check_strength(thisval,thisid);

});



$(document.body).on('click', '.'+this.options.strengthButtonClass, function(e) {
e.preventDefault();

thisclass = 'hide_'+$(this).attr('class');
thisclass = 'hide_'+$(this).attr('class');



Expand All @@ -130,16 +139,16 @@
$('input[type="password"][data-password="'+thisid+'"]').hide();
$('a[data-password-button="'+thisid+'"]').addClass(thisclass).html(strengthButtonTextToggle);
isShown = true;

}



});




},

yourOtherFunction: function(el, options) {
Expand All @@ -158,5 +167,3 @@
};

})( jQuery, window, document );


2 changes: 1 addition & 1 deletion src/strength.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.