mardi 10 mars 2015

Byte counting in javascript


Vote count:

0




I am making a word count, more specifically btye count for my text editor. However, I'm making it so that it counts Korean as well (as 2 bytes).


But I need to make it so that it does not count characters such as ` # = { } ( ) etc...


I've gotten this far, but I do not know how to get further from here. Right now it does everything but what I want it to do. Thank you!



var limit_length = 1000;
var msg_length = 0;

//String bytes() Function
String.prototype.bytes = function() {
var msg = this;
var cnt = 0;

//Korean 2, English 1 count increment
for( var i=0; i< msg.length; i++)
cnt += (msg.charCodeAt(i) > 128 ) ? 2 : 1;
return cnt;
}

//#submit-content Key up activation
$("#submit-content").keyup(function( e ){
msg_length = $.trim($(this).val()).bytes(); //to trim whitespaces

if( msg_length <= limit_length ) {
$("#type_num").css("color", "#474646");
$("#type_num").html( msg_length );
}
else {
$("#type_num").css("color", "#E55451");
$("#type_num").html( msg_length );
}
});


asked 33 secs ago







Byte counting in javascript

Aucun commentaire:

Enregistrer un commentaire