Created
March 28, 2020 19:05
-
-
Save sleepdefic1t/7123f9136e528fa591e8d4da9d5bae6d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Check that a Strings is alphanumeric (0-9 | a-z | A-Z) | |
* | |
* Faster than regex | |
* - src: https://stackoverflow.com/a/25352300 | |
* | |
* @param str | |
* @returns {boolean} true if alphanum., false if not alphanum. or length is 0 | |
*/ | |
export function isAlphaNumeric(str) { | |
if (str.length === 0) { | |
return false; | |
} | |
var code, i, len; | |
for (i = 0, len = str.length; i < len; i++) { | |
code = str.charCodeAt(i); | |
if (!(code > 47 && code < 58) && // numeric (0-9) | |
!(code > 64 && code < 91) && // upper alpha (A-Z) | |
!(code > 96 && code < 123)) { // lower alpha (a-z) | |
return false; | |
} | |
} | |
return true; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment