Input encoding issue with a jquery word counter

Konata

Alright, so I have this script here jsfiddle.net/CDLtn/2/
that counts up the words and displays a value based on if any of the checkboxes were checked it works fine except one thing, it doesen't work with Russian input.

$(function () {
var wordCounts = {};
$("input[type='text']:not(:disabled)").keyup(function () {
var matches = this.value.match(/\b/g);
wordCounts[this.id] = matches ? matches.length / 2 : 0;
var finalCount = 0;
var x = 0;
$('input:checkbox:checked').each(function () {
    x += parseInt(this.value);
});
x = (x == 0) ? 1 : x;
$.each(wordCounts, function (k, v) {
    finalCount += v * x;
});
$('#finalcount').val(finalCount)
}).keyup();
$('input:checkbox').change(function () {
$('input[type="text"]:not(:disabled)').trigger('keyup');
});
});

I've found an open source counter http://roshanbh.com.np/2008/10/jquery-plugin-word-counter-textarea.html and this one does accept Russian input ( here is a fiddle of the link above jsfiddle.net/Joniniko/TyPSJ/ )

I need to either somehow make my original counter work with Russian input, or maybe incorporate the checkbox feature into the one that was made by Roshan.

Here is an example of russian text just incase "Привет как дела"

(My source page encoding has been changed to UTF-8 already, and ive also tried other ones for cyrillic input)

UPD: jsfiddle.net/Joniniko/CDLtn/5/ this accepts the russian input, but it increments the counter by 0.5 instead of 1 for some unknown to me reason

slashingweapon

The problem is the way you have defined "words". The \b regex escape sequence only recognizes [a-zA-Z0-9] as "word characters".

> "Привет как дела".match(/\b/g);
null

I think what you want to do instead is split your words along spaces:

> "Привет как дела".split(/\s+/);
["Привет", "как", "дела"]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related