Stories. Questions. Different perspectives.
Browse WAURE
← Advice & Questions
Technology · Advice & Questions ·

Why does one emoji use several characters in our form's counter?

Technology category photographTechnology

Our short greeting form promises “40 characters”. In the JavaScript counter I used the text's length property. A family emoji looks like one symbol in the box, but the counter jumps by several, which makes the limit hard to explain.

MDN says JavaScript string length counts UTF-16 code units. It distinguishes that from counting grapheme clusters, the units closer to what a reader may perceive as characters. MDN's string-length reference

The limit is meant to keep the printed greeting compact, not to meet a storage allowance. Should we change how we count, change the wording of the limit, or use a different rule? I want the form's promise to match what it actually enforces.

Discussion

5 community replies

Decide what the limit is trying to control before changing the counter. If you mean symbols a person perceives as characters, a code-unit count is a poor explanation of that promise. If you mean printed width, even a better character count won't directly measure the layout.

MDN points to Intl.Segmenter for grapheme-cluster counting. That addresses a different unit from the length property. It doesn't mean every sequence that looks simple will consist of a single Unicode code point.

For this greeting, I'd show a preview of the actual printable area as well as any text limit. Forty narrow letters and forty wide ones aren't a promise of the same printed result. The counter should serve the design goal rather than become a puzzle the contributor has to solve.

Check that the front-end counter and the receiving system enforce the same chosen rule. A friendly counter is frustrating if the submitted greeting is later rejected by a different length calculation.

Use representative text in that check, including the emoji that exposed the mismatch and combining marks in names. Document the expected result for each example. Testing only plain English letters would make two different counting rules look more consistent than they are.

Join the discussion

More in Technology