I try to compare two string first ASCII (English) and second Unicode (Chinese). User need to type message in textarea and JavaScript will calculate how long the text and show some message.
Now its working only for ASCII (English) but I need to compare because condition for ASCII and Unicode is different.
Below is my current code snippet:
<script>
$('#myinput').keyup(function() {
if(this.value.length<=152)
$('#charcount').text('We will deduct 1 credit for this message per contact'),
$('#credit_value').val('1');
else if(this.value.length>152 && this.value.length<298)
$('#charcount').text('We will deduct 2 credit for this message per contact'),
$('#credit_value').val('2');
else if (this.value.length>=298 && this.value.length<450)
$('#charcount').text('We will deduct 3 credit for this message per contact'),
$('#credit_value').val('3');
else if (this.value.length>=450 && this.value.length<596)
$('#charcount').text('We will deduct 4 credit for this message per contact'),
$('#credit_value').val('4');
else if (this.value.length>=602 && this.value.length<754)
$('#charcount').text('We will deduct 5 credit for this message per contact'),
$('#credit_value').val('5');
})
</script>
For ASCII (English) I start with 152 but in Unicode (Chinese) I need to start with 60 and increase ...
Below is the code that I'm using in PHP to check text/string in English or Chinese:
// check if message is type in chinese or english/UTF8
if (preg_match("/\p{Han}+/u", $message)) {
$msg_type = '2';
//this is chinese
} else {
$msg_type = '1';
//this is UTF8/English
}
So my problem now I need to check the string is type in ASCII or Unicode then if Chinese start condition value with 60 or if English start with 152.
Check my jsfiddle here.