1
function countChar(val)
{
var xy=document.getElementsByID(val).value;
var len = xy.length;
if (len >= 2) {
val.value = val.value.substring(0, 1);
} else {
document.getElementsByID(val).value=(1 - len);
}
}
<input type="text" name="mytextbox" id="mytextbox"
onKeyUp="countChar(this.value)" size="1" maxlength="1" value="" />