最初のASCII(英語)と2番目のUnicode(中国語)の2つの文字列を比較しようとしています。ユーザーはtextareaにメッセージを入力する必要があり、javascriptはテキストの長さを計算し、メッセージを表示します。
現在はASCII(英語)でのみ機能しますが、ASCIIとUnicodeの条件が異なるため、比較する必要があります。
以下は私の現在のコードスニペットです:
$('#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>
ASCII(英語)の場合は152から始めますが、Unicode(中国語)の場合は60から始めて増やす必要があります...
以下は、英語または中国語のテキスト/文字列をチェックするためにphpで使用しているコードです。
// 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
}
したがって、私の問題は、文字列がASCIIまたはUnicodeで入力されていることを確認する必要がある場合、中国語の開始条件値が60であるか、英語が152で始まるかどうかを確認する必要があります。
ここで私のjsfiddleをチェックしてください