-2

I want to add client side validation for counting characters. That field limit is 300 in database. At server side validation, I get proper string length but while saving form as draft, server side validation is bypassed.

So i get exception. Can anyone help to check character length for Latin characters like

£, §, £, Ä

For Example:

String s= "wa£erw£";

System.out.println("SiZE "+s.length());

System.out.println("UTF - SiZE "+s.getBytes(java.nio.charset.StandardCharsets.UTF_8).length );

Output:

SiZE 7
UTF - SiZE 9

I need Count 9 using JavaScript

Neha Shettar
  • 703
  • 10
  • 28

1 Answers1

1

Lets clear some things - the string "wa£erw£":

  • contains 7 characters

  • has 9 bytes in UTF-8, be cause of the 2 multi-byte chars in it - these 2 chars will be 2 bytes each and total is 9 bytes.

In JavaScript .length() returns the number of characters, so the first result is right. Reference: JavaScript String length Property

s.getBytes(java.nio.charset.StandardCharsets.UTF_8).length - this will first convert the string to byte array, and then returns the length of this byte array. As per above the byte array will be 9 bytes. So second is also correct.

If you want to get the number of bytes in JS, it is not so easy. You can find some sort of solution in this discussion: String length in bytes in JavaScript

The better approach is to store your string data correctly in your database with same encoding for the whole app (like using UTF-8 in DB for example). Then you will get rid of all overhead of re-encoding texts, validation issues like this and etc. You can also have bad encoding issues if your DB columns differs you remaining app.

Vasil Popov
  • 1,210
  • 14
  • 22