var numbers = "0123456789", letters = "abcdefghijklmnñopqrstuvwxyz", caps = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZ"; var passwordInput = document.getElementById("password"); function passwordStrengthText(value, locale) { var text = ''; if (locale === 'es') { text += "Añade "; } else { text += "Add "; } if (!hasLetters(value) && locale === 'es') { text += "minúsculas, "; } else if (!hasLetters(value)) { text += "lowercase, "; } if (!hasNumbers(value) && locale === 'es') { text += "números, "; } else if (!hasNumbers(value)) { text += "numbers, "; } if (!hasCaps(value) && locale === 'es') { text += "mayúsculas, "; } else if (!hasCaps(value)) { text += "caps, "; } if (!hasSymbols(value) && locale === 'es') { text += "signos de puntuación, "; } else if (!hasSymbols(value)) { text += "symbols, "; } if(!hasMinimunCharacters(value) && locale === 'es') { text += "8 o más caracteres"; } else if (!hasMinimunCharacters(value)) { text += "8 or more characters "; } return text.trim().replace(/(^,)|(,$)/g, "") + "."; } function hasCaps(text) { for (i = 0; i < text.length; i++) { if (caps.indexOf(text.charAt(i), 0) != -1) { return 1; } } return 0; } function hasNumbers(text) { for (i = 0; i < text.length; i++) { if (numbers.indexOf(text.charAt(i), 0) != -1) { return 1; } } return 0; } function hasLetters(text) { for (i = 0; i < text.length; i++) { if (letters.indexOf(text.charAt(i), 0) != -1) { return 1; } } return 0; } function hasSymbols(str) { return (/[~`*\\(\\)_@!#$%\^&*+=\-\[\]\\';,./{}|\\":<>\?]/g.test(str)) ? 1 : 0; } function hasMinimunCharacters(str) { return (str.length >= 8) ? 1 : 0; } var PSM = { messages: { 'invalid': { 'en': '8 characters minimun', 'es': 'Mínimo 8 caracteres' }, 'strong': { 'en': 'Very well, you made it', 'es': 'Muy bien, lo has logrado!' } }, strengthTexts: { 'invalid': { 'en': 'Too short', 'es': 'Muy corta' }, 'weak': { 'en': 'Short', 'es': 'Corta' }, 'acceptable': { 'en': 'Too weak', 'es': 'Muy Débil' }, 'good': { 'en': 'Weak', 'es': 'Débil' }, 'strong': { 'en': 'Strong', 'es': 'Fuerte' } }, setLocale: function (locale) { this.locale = locale; }, init: function (locale) { if (locale) { this.setLocale(locale.toLowerCase()); } else { this.setLocale('es'); } var self = this; var psm = $(".password-strength-meter"); var psi = $(".password-strength-input"); var pst = $(".password-strength-text"); var pht = $(".password-helper-text"); document.querySelector(".password-strength-input").addEventListener("keyup", function (event) { var strengthLevel = 0; event.stopPropagation(); if (this.value.length === 0) { psm.removeClass().addClass("password-strength-meter"); pst.html(''); pht.html(''); return; } //if (hasMinimunCharacters(this.value)) { strengthLevel = hasLetters(this.value) + hasNumbers(this.value) + hasCaps(this.value) + hasSymbols(this.value) + hasMinimunCharacters(this.value); //} switch (strengthLevel) { case 1: psm.removeClass().addClass("password-strength-meter password-invalid").css( "width", "calc(20% - 3rem)" ); psi.addClass("thin-line"); pst.html(self.messages.invalid[self.locale]).css( "color", "red" ); pht.html(self.strengthTexts.invalid[self.locale]); break; case 2: psm.removeClass().addClass("password-strength-meter password-invalid").css( "width", "calc(40% - 3rem)" ); psi.addClass("thin-line"); pst.html(passwordStrengthText(this.value, self.locale)).css( "color", "red" ); pht.html(self.strengthTexts.weak[self.locale]); break; case 3: psm.removeClass().addClass("password-strength-meter password-invalid").css( "width", "calc(60% - 3rem)" ); psi.addClass("thin-line"); pst.html(passwordStrengthText(this.value, self.locale)).css( "color", "red" ); pht.html(self.strengthTexts.acceptable[self.locale]); break; case 4: psm.removeClass().addClass("password-strength-meter password-invalid").css( "width", "calc(80% - 3rem)" ); psi.addClass("thin-line"); pst.html(passwordStrengthText(this.value, self.locale)).css( "color", "red" ); pht.html(self.strengthTexts.good[self.locale]); break; case 5: psm.removeClass().addClass("password-strength-meter password-strong").css( "width", "calc(100% - 3rem)" ); psi.addClass("thin-line"); pst.html(self.messages.strong[self.locale]).css( "color", "green" ); pht.html(self.strengthTexts.strong[self.locale]); break; default: psm.removeClass().addClass("password-strength-meter password-invalid").css( "width", "calc(20% - 3rem)" ); psi.addClass("thin-line"); pst.html(self.messages.invalid[self.locale]).css( "color", "red" ); pht.html(self.strengthTexts.invalid[self.locale]); break; } }); document.querySelector(".password-strength-input").addEventListener("focusin", function () { //pst.show(); if (this.value.length === 0) { pst.html(self.messages.invalid[self.locale]); } else { psi.addClass("thin-line"); } }); document.querySelector(".password-strength-input").addEventListener("focusout", function () { //pst.hide(); psi.removeClass("thin-line"); }); } }; $(document).ready(function () { $('.password-helper').on('click', function () { if (!$(this).hasClass('see-pass')) { passwordInput.type = "text"; $(this).html('').addClass('see-pass'); } else { passwordInput.type = "password"; $(this).html('').removeClass('see-pass'); } }); });