Skip to content

Instantly share code, notes, and snippets.

@christophemarois
Forked from anonymous/jsbin.yupujiro.css
Created March 17, 2014 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophemarois/9603629 to your computer and use it in GitHub Desktop.
Save christophemarois/9603629 to your computer and use it in GitHub Desktop.
input[type="text"][role="password"] {
width: 200px;
padding: 5px 7px;
font-size: 14px;
}
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-git2.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<input type="text" role="password">
</body>
</html>
$(function(){
$('input[type="text"][role="password"]').modernPasswordInput();
});
$.fn.modernPasswordInput = function(passChar) {
var passChar = passChar || '•';
var $els = this.filter('input[type="text"]');
var generateNChars = function(c, n) {
var a="";for(i=1;i<=n;i++){a=a+c} return a;
};
$els.each(function(){
var el = this, $el = $(this);
el.timer = false;
el.orgValue = el.value;
el.orgLength = el.value.length;
$el.on('input', function(){
clearTimeout(el.timer);
var caretIndex = el.selectionStart <= 0 ? 0 : el.selectionStart,
newLength = el.value.length;
if (caretIndex < el.value.length) {
el.orgValue = el.value = ""; el.orgLength = 0;
return;
}
if ( newLength > el.orgLength ) {
var lastChar = el.value.substr(-1);
el.orgValue = el.orgValue + lastChar;
el.value = generateNChars(passChar, el.value.length - 1) + lastChar;
el.timer = setTimeout(function(){
el.value = generateNChars(passChar, el.value.length);
}, 300);
} else {
el.orgValue = el.orgValue.substr(0, newLength);
el.value = generateNChars(passChar, el.value.length);
}
el.orgLength = el.value.length;
})
});
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment