Skip to content

Instantly share code, notes, and snippets.

@shamique
Last active February 3, 2019 07:20
Show Gist options
  • Save shamique/f5b75e5390f2de5a8484b16f38b915f0 to your computer and use it in GitHub Desktop.
Save shamique/f5b75e5390f2de5a8484b16f38b915f0 to your computer and use it in GitHub Desktop.
var actualPassword = [],
temperoryPassword,
currentCursorPosition = 0,
passwordChar = '•';
$("#password-text").bind("input", function () {
temperoryPassword = $(this).val();
var passwordLength = temperoryPassword.length;
for (var i = 0; i < passwordLength; i++) {
if (temperoryPassword[i] != passwordChar) {
actualPassword[i] = temperoryPassword[i];
}
}
// Get current cursor position.
currentCursorPosition = this.selectionStart;
$(this).val(temperoryPassword.replace(/./g, passwordChar));
});
$("#password-text").bind("keyup", function () {
var passwordLength = $(this).val().length;
if (passwordLength < actualPassword.length) {
var difference = actualPassword.length - passwordLength,
key = event.keyCode || event.charCode;
// Check if last keypress was backspace or delete
if (key == 8 || key == 46) {
actualPassword.splice(currentCursorPosition, difference);
}
// User highlighted and overwrite part of the password
else {
actualPassword.splice(currentCursorPosition - 1, difference + 1);
actualPassword.splice(currentCursorPosition - 1, 0, temperoryPassword[currentCursorPosition - 1]);
}
}
});
$("#btnSubmit").click(function(){
$("#passwordTxt").text(bindactualPassword(actualPassword));
});
// disable password cut, copy and paste function in password field
$('#password-text').bind("cut copy paste",function(e) {
e.preventDefault();
});
function bindactualPassword(){
return actualPassword.join("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment