Since the Unicode code point of the character being inserted is passed with the keypress event, we can simply turn it into a string and toss it into jquery.byteLength to calculate the length in bytes.
Also fixed a broken test case that had the wrong number of expected chars.
Note that this will probably still not work right in many circumstances -- pastes, anything dealing with selection, and it's entirely possible that individual 'keypress' events might be totally unreliable when dealing with funky input methods.
}
var len = $.byteLength( this.value );
+ // Note that keypress returns a character code point, not a keycode.
+ // However, this may not be super reliable depending on how keys come in...
+ var charLen = $.byteLength( String.fromCharCode( e.which ) );
- // limit-3 as this doesn't count the character about to be inserted.
- if ( len > ( limit-3 ) ) {
+ if ( ( len + charLen ) > limit ) {
e.preventDefault();
}
});
sample: mbSample,
useLimit: true,
limit: 12,
- expected: 10 // 10 x 1-byte char. The next 3-byte char exceeds limit of 12
+ expected: 12 // 10 x 1-byte char. The next 3-byte char exceeds limit of 12, but 2 more 1-byte chars come in after.
});