html - JavaScript Unicode's length (astral symbols) -


i have < input type="text" > (in html) , everytime add character if text.length < x {...} (in javascript).

the problem unicode special characters/astral symbols (\u{.....}, ones more 4 hex/ non-bmp characters) "are stored 2 code units , length property return 2 instead of 1."

(https://mixmax.com/blog/unicode-woes-in-javascript)

i wanna able 1 symbols or 2, long doesn't mix 1 , 2 because have have working limit on size of visual text.

i think solutions here: https://mathiasbynens.be/notes/javascript-unicode#accounting-for-astral-symbols i'm not sure how use that.

my if this:

if(document.getelementbyid("1").value.length<16){ 

edit (it's working!):

<html>     <head>         <style>             input{background:white;border:1px solid;height:30;outline-color:black;position:absolute;top:389;width:30}         </style>         <script>             <!--                 function add(symbol){                     if (countsymbols(document.getelementbyid("1").value)<16) {                         document.getelementbyid("1").value+=symbol}                     if(document.getelementbyid("1").value.length==16 && document.getelementbyid("1").value=="\u{1f4bb}\u{1f3ae}\u{1f3c3}\u{1f525}\u2764\u{1d7cf}\u{1d7d1}\u{1f4b0}\u2757"){                         document.getelementbyid("1").style.background="#00bb00"}                     if(document.getelementbyid("1").value.length==16 && document.getelementbyid("1").value!="\u{1f4bb}\u{1f3ae}\u{1f3c3}\u{1f525}\u2764\u{1d7cf}\u{1d7d1}\u{1f4b0}\u2757"){                         document.getelementbyid("1").style.background="#bb0000"}                 }                 function countsymbols(string) {                     var regexastralsymbols = /[\ud800-\udbff][\udc00-\udfff]/g;                     return string                     // replace every surrogate pair bmp symbol.                     .replace(regexastralsymbols, '_')                     // …and *then* length.                     .length;                 }             //-->         </script>     </head>     <body>         <input readonly="true" id="1" style="left:573;outline:0;padding:5 8;top:356;width:294">         <input onclick="add('\u{1f4bb}')" style="left:573" type="button" value="&#128187">         <input onclick="add('\u{1f3ae}')" style="left:606" type="button" value="&#127918">         <input onclick="add('\u{1f3c3}')" style="left:639" type="button" value="&#127939">         <input onclick="add('\u{1f525}')" style="left:672" type="button" value="&#128293">         <input onclick="add('\u2764')" style="left:705" type="button" value="&#10084">         <input onclick="add('\u{1d7cf}')" style="left:738" type="button" value="&#120783">         <input onclick="add('\u{1d7d1}')" style="left:771" type="button" value="&#120785">         <input onclick="add('\u{1f4b0}')" style="left:804" type="button" value="&#128176">         <input onclick="add('\u2757')" style="left:837" type="button" value="&#10071">     </body> </html> 

i think have of research done, need put of together:

taking function link provides:

function countsymbols(string) {     var regexastralsymbols = /[\ud800-\udbff][\udc00-\udfff]/g;     return string         // replace every surrogate pair bmp symbol.         .replace(regexastralsymbols, '_')         // …and *then* length.         .length; } 

your if should be

if (countsymbols(document.getelementbyid("1").value)<16) { ...} 

for example: countsymbols('🏃2🔥7') returns 4

here have small example: https://jsfiddle.net/q7g9qtk7/

update: can use array.from (polyfilling ie, chrome , firefox support it), takes string , splits each character, no matter how long is:

array.from('🏃2🔥7') //returns ["🏃", "2", "🔥", "7"]

so function

function countsymbols(string) {        return array.from(string).length; } 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -