I need to limit input box content based on lang. enter.
For example:-
If a string with Korean characters is input, then the number of permitted characters is 8. If a string with Chinese characters is input, the number of permitted characters is 5. If that with English, then 12 characters are permitted.
My code is working well for English characters in IE, Firefox and Chrome. However, this code is not working as expected for Korean and Chinese characters. My code always cuts the length of string to 2 even if i increase the valid length. Please suggest some solution as soon as possible.
I am pasting my code for checking.
<!DOCTYPE html>
<html>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
document.onkeydown=function() {
var text = $('#lang').val();
var hangul = new RegExp("[\u1100-\u11FF|\u3130-\u318F|\uA960-\uA97F|\uAC00-\uD7AF|\uD7B0-\uD7FF]");
var china = new RegExp("[\u4E00-\u9FFF|\u2FF0-\u2FFF|\u31C0-\u31EF|\u3200-\u9FBF|\uF900-\uFAFF]");
// alert(hangul.test(text));
if(hangul.test(text))
{
limit = 8;
//console.log("korean");
limitCharacters('lang', limit , text);
}else if(china.test(text))
{
limit = 5;
//console.log("china");
limitCharacters('lang', limit , text);
}else {
limit = 11;
limitCharacters('lang', limit , text);
}
};
function limitCharacters(textid, limit, text)
{
//alert('here in limit funt.');
var textlength = text.length;
//alert(textlength);
if(textlength > limit )
{
$('#'+textid).val(text.substr(0,limit));
return false;
}else {
$('#'+textid).val(text);
$('#txt').html(text);
return true;
}
}
</script>
<body>
<input type="text" id="lang" />
</body>
</html>
limit
to, say, 100? What happens if you type eight English letters and then a Chinese character? – Vodkaalert
orconsole.log
. Even if I copy and paste string in any language to the input box it works fine... but writing in input box with Korean characters not working. The problem is I couldn't able to make out why it is cutting Korean characters limit. – Reparablenavigator.language
and use that in your input logic – Reminiscence