Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string is an html font in JavaScript

I am making a robotic "talk and respond" console, and I want to be able to change the font in the console. I know how to change the font, but I do not want to change the font if the given font is not an actual HTML font.

Example: The user enters Arial, the command goes through. The user enters I lost my mommy, the command does not go through and the computer alerts the user that the command is invalid.

I obviously do not want to create a very large array containing all of the HTML fonts. That would not make sense.

If a jQuery solution works, then let me know. I am using jQuery in my code.

like image 210
Hazard Avatar asked Nov 23 '25 23:11

Hazard


1 Answers

JavaScript/CSS Font Detector is a library that might help.

Include the library and call

detective.detect('[font name]'));

It isn't true font identification, but it may work for what you are trying to accomplish.

detect returns a boolean value based on if the string value provided matches a font that is available in the browser.

 function isFont(fontName) {
      return fontName ? detective.detect(fontName) : false;
 }
like image 193
Jerhemy Avatar answered Nov 26 '25 11:11

Jerhemy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!