jquery maxlength "æ" or "ø" or "å" should count as two characters

character, jquery, maxlength, validation

Solution

Try something like this:

function countChar(mystring){
    var count=0;
    for (var i=0;i<mystring.length;i++)
    {
        if (mystring[i].match(/[æøåÆØÅ]/))
            count += 2;
        else
            count++;
    }
    return count;
}

Problem

I want to send text through a sms gateway, and they have a limit of 11 characters, in the from field. and æ or ø or å count as two characters. I use jquery validator, to count, and show user message, how do I get maxlength, to minus two every time, the users enter æ or ø or å, and only minus one character, when it's from a-z ?? Right now the script, allow a-z and æøå, 0-9 and space, but it don't count æøå as two characters. how do I do that? ``` jQuery.validator.addMethod("accept", function(value, element, param) { return value.match(new RegExp("^" + param + "$")); }); from: { required: false, accept: "[a-zA-Z0-9æøåÆØÅ ]+", minlength: 2, maxlength: 11 }, ```

Original source