John
John

Reputation: 164

character type detection

Below is what I've already written and it's working fine:

var $remaining = $('#remaining'),
$messages = $remaining.next();

$('#message').keyup(function(){
var chars = this.value.length,
    messages = Math.ceil(chars / 160),
    remaining = messages * 160 - (chars % (messages * 160) || messages * 160);

$remaining.text(remaining + ' characters remaining');
$messages.text(messages + ' message(s)');

}); This is the above code in action

And now what I'm trying to do is :

var text_max_En = 100;
var text_max_utf8 = 200;
if   /*  what user has typed is English   */
{
   Do this
}
else if /*  what user has typed is in utf8 type*/
{
   Do that
}

In short :

**If** user has typed there in English then it should count up to 100 characters per message

AND **if** that's in urf8 type then it should count up to 200 ones.

Any solution?

Upvotes: 0

Views: 79

Answers (1)

Scott Mermelstein
Scott Mermelstein

Reputation: 15397

Use String.charCodeAt to get the numeric value of any or all characters in a string. If the value is > 127, then you're dealing with unicode values.

Since you're callback is keyUp, you can do this one at a time:

var str = $('#textarea').val();
var ascii = str.charCodeAt(str.length - 1) < 128;

(You can also just loop through the string length, but that's redundant, since you're acting one keystroke at a time.)

Upvotes: 1

Related Questions