How do I detect if a character, like "", is a unsupported one with JavaScript?

Viewed 401

Sometimes people fill in forms with weird stuff like strange Unicode combinations in order to show special fonts.

For instance:

However, sometimes the characters are not supported by the client, and thus result in a "".

How can I detect the failure with JavaScript? Is this possible?

Thanks

2 Answers

The best way is to do it in the other way:

  • you select the webfont, and so the allowed ranges. Webfont description gives you the allowed character list, usually designers will enhance, and never remove glyphs.

  • you allow only such characters in your form.

Otherwise it will depend on the web browser. The same user could see different outcome if it is in different browser (e.g. mobile vs. computer). So the best way (IMHO) it is to remove client fonts altogether from the rendering, and so you explicitly specify the glyphs (and how to display it) by using a webfont. This is independent of client/webbrowser (but on very old ones)

You can detect non-characters, that fulfills the use case given in the question.

use utf8;
use Unicode::UCD qw(charinfo);
if (defined charinfo ord '') {
    print 'is a character';
} else {
    print 'is a non-character';
}

See https://npmjs.com/search?q=ucd for JavaScript UCD libraries.

Related