Created
March 17, 2014 17:04
-
-
Save anonymous/9603609 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input[type="text"][role="password"] { | |
width: 200px; | |
padding: 5px 7px; | |
font-size: 14px; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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