Skip to content

Instantly share code, notes, and snippets.

@sleepdefic1t
Created March 28, 2020 19:05
Show Gist options
  • Save sleepdefic1t/7123f9136e528fa591e8d4da9d5bae6d to your computer and use it in GitHub Desktop.
Save sleepdefic1t/7123f9136e528fa591e8d4da9d5bae6d to your computer and use it in GitHub Desktop.
/**
* 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