Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #37600 > unrolled thread
| Started by | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| First post | 2019-10-23 17:12 +1100 |
| Last post | 2019-10-23 09:40 -0600 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.javascript
Input field with a single character Andrew Poulos <ap_prog@hotmail.com> - 2019-10-23 17:12 +1100
Re: Input field with a single character "R.Wieser" <address@not.available> - 2019-10-23 08:24 +0200
Re: Input field with a single character JJ <jj4public@vfemail.net> - 2019-10-23 15:28 +0700
Re: Input field with a single character Jon Ribbens <jon+usenet@unequivocal.eu> - 2019-10-23 12:24 +0000
Re: Input field with a single character Martin Leese <please@see.Web.for.e-mail.INVALID> - 2019-10-23 09:40 -0600
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2019-10-23 17:12 +1100 |
| Subject | Input field with a single character |
| Message-ID | <44qdnScci8v4cjLAnZ2dnUU7-bnNnZ2d@westnet.com.au> |
I had a text field with max="1" in which the user is to enter a single
"symbol" (such as an exclamation mark, period, single letter, single
numeral...) It also needed to accept single emoji.
On Windows with Chrome an emoji has a length of 2 (at least the ones I
tested have) so I changed the field to max="2". It needs to return a
single "symbol" so I need to be able to tell two characters from a
single emoji;
I googled and found this
"😸".codePointAt(0);
// "emoji".codePointAt(0);
which returns 128568 but that took me nowhere!
More googling turned about functions that can test for emoji but the
comments on them indicate that they all have issues. Except for the
lodash library which has this
(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?(?:\u200d(?:[^\ud800-\udfff]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?)*
Is the lodash library regex what I should use?
Andrew Poulos
[toc] | [next] | [standalone]
| From | "R.Wieser" <address@not.available> |
|---|---|
| Date | 2019-10-23 08:24 +0200 |
| Message-ID | <qoornl$1g3d$1@gioia.aioe.org> |
| In reply to | #37600 |
Andrew, > I googled and found this > "??".codePointAt(0); > // "emoji".codePointAt(0); > which returns 128568 but that took me nowhere! You need the UTF-8 characterset*. Just like the codepoint 65 indicates the letter "A" all other codepoints indicate other symbols (though some codepoints will be represented by the same symbol). * like this one: http://isthisthingon.org/unicode/index.phtml In short, all you /can/ do is to figure out al the codepoints you wish to accept and check for them. > Is the lodash library regex what I should use? No idea. But looking at it it checks for ranges, which is, apart from the range 32 .. 126 (the standard keyboard symbols), the opposite of what you are after. Regards, Rudy Wieser
[toc] | [prev] | [next] | [standalone]
| From | JJ <jj4public@vfemail.net> |
|---|---|
| Date | 2019-10-23 15:28 +0700 |
| Message-ID | <1tiys21yrpsla$.wsxuf3dw2q9g.dlg@40tude.net> |
| In reply to | #37600 |
On Wed, 23 Oct 2019 17:12:50 +1100, Andrew Poulos wrote:
> I had a text field with max="1" in which the user is to enter a single
> "symbol" (such as an exclamation mark, period, single letter, single
> numeral...) It also needed to accept single emoji.
>
> On Windows with Chrome an emoji has a length of 2 (at least the ones I
> tested have) so I changed the field to max="2". It needs to return a
> single "symbol" so I need to be able to tell two characters from a
> single emoji;
>
> I googled and found this
> "������".codePointAt(0);
> // "emoji".codePointAt(0);
> which returns 128568 but that took me nowhere!
>
> More googling turned about functions that can test for emoji but the
> comments on them indicate that they all have issues. Except for the
> lodash library which has this
>
> (?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?(?:\u200d(?:[^\ud800-\udfff]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?)*
>
> Is the lodash library regex what I should use?
>
> Andrew Poulos
Use `codePoint()` to count the length of the text. e.g.
function textLength(s) {
var i, l = 0;
for (i = 0; i < s.length; i += s.codePointAt(i) > 0xffff ? 2 : 1) l++;
return l;
}
var s;
console.clear();
s = "\u{1f638}"; //grinning cat face
console.log('s =', s, '=', textLength(s)); //1
s = "\u2602"; //umbrella
console.log('s =', s, '=', textLength(s)); //1
s = "A"; //"A"
console.log('s =', s, '=', textLength(s)); //1
var s = "\u{1f638}A\u2602"; //grinning cat face, "A", umbrella
console.log('s =', s, '=', textLength(s)); //3
[toc] | [prev] | [next] | [standalone]
| From | Jon Ribbens <jon+usenet@unequivocal.eu> |
|---|---|
| Date | 2019-10-23 12:24 +0000 |
| Message-ID | <slrnqr0hjk.5kc.jon+usenet@sable.unequivocal.eu> |
| In reply to | #37606 |
On 2019-10-23, JJ <jj4public@vfemail.net> wrote:
> Use `codePoint()` to count the length of the text. e.g.
>
> function textLength(s) {
> var i, l = 0;
> for (i = 0; i < s.length; i += s.codePointAt(i) > 0xffff ? 2 : 1) l++;
> return l;
> }
>
> var s;
> console.clear();
> s = "\u{1f638}"; //grinning cat face
> console.log('s =', s, '=', textLength(s)); //1
> s = "\u2602"; //umbrella
> console.log('s =', s, '=', textLength(s)); //1
> s = "A"; //"A"
> console.log('s =', s, '=', textLength(s)); //1
> var s = "\u{1f638}A\u2602"; //grinning cat face, "A", umbrella
> console.log('s =', s, '=', textLength(s)); //3
I'm being slightly mean, but just to illustrate that "what is the
length of a Unicode string" is not a simple question:
> s = '\u00e1'
> console.log('s =', s, '=', textLength(s))
s = á = 1
> s = 'a\u0301'
> console.log('s =', s, '=', textLength(s))
s = á = 2
> s = s.normalize('NFKC') // only available in ES6
> console.log('s =', s, '=', textLength(s))
s = á = 1
[toc] | [prev] | [next] | [standalone]
| From | Martin Leese <please@see.Web.for.e-mail.INVALID> |
|---|---|
| Date | 2019-10-23 09:40 -0600 |
| Message-ID | <qops8u$s1p$1@dont-email.me> |
| In reply to | #37600 |
Andrew Poulos wrote: > I had a text field with max="1" in which the user is to enter a single > "symbol" (such as an exclamation mark, period, single letter, single > numeral...) It also needed to accept single emoji. > > On Windows with Chrome an emoji has a length of 2 (at least the ones I > tested have) so I changed the field to max="2". It needs to return a > single "symbol" so I need to be able to tell two characters from a > single emoji; Maybe you need a length of 3 :-) -- Regards, Martin Leese E-mail: please@see.Web.for.e-mail.INVALID Web: http://members.tripod.com/martin_leese/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web