$(document).ready(function () {
$(\"#id\").keydown(function () {
});
})
This code perfectly works for everything(number,alphabet,symbol et
I had the same issue and I solved it using the input event.
//calculate the length of a character
function getLen(str){
var result = 0;
for(var i=0;i= 0x00 && chr < 0x81) ||
(chr === 0xf8f0) ||
(chr >= 0xff61 && chr < 0xffa0) ||
(chr >= 0xf8f1 && chr < 0xf8f4)){
//half width counted as 1
result += 1;
}else{
//full width counted as 2
result += 2;
}
}
return result;
};
// trim the string by processing character by character
function getTrimmedString(theString, maxLength){
var tempLength = 0;
var trimmedString = "";
for (var i = 0; i < theString.length; i++) {
tempLength = getLen(theString.charAt(i)) + tempLength;
if(tempLength > maxLength){
break;
}else{
trimmedString = trimmedString + theString.charAt(i);
}
}
return trimmedString;
}
// limit the size of a field
function limitCity(){
var maxChars = 30;
var cityVal = $("#city").val();
var cityLength = getLen(cityVal);
if (cityLength >= maxChars) {
var trimmedString = getTrimmedString(cityVal, maxChars);
$("#city").val(trimmedString);
}
}
//bind the input event
$("#city").bind("input", limitCity);