The asker's original inclination to use str.charCodeAt(i) appears to be faster than the regular expression alternative. In my test on jsPerf the RegExp option performs 66% slower in Chrome 36 (and slightly slower in Firefox 31).

Here's a cleaned-up version of the original validation code that receives a string and returns true or false:

function isAlphaNumeric(str) {
  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;
};

Of course, there may be other considerations, such as readability. A one-line regular expression is definitely prettier to look at. But if you're strictly concerned with speed, you may want to consider this alternative.

Answer from Michael Martin-Smucker on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-validate-an-input-is-alphanumeric-or-not-using-javascript
How to Validate an Input is Alphanumeric or not using JavaScript? - GeeksforGeeks
July 12, 2025 - To validate alphanumeric in JavaScript, regular expressions can be used to check if an input contains only letters and numbers.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-validate-an-input-is-alphanumeric-or-not-using-javascript
How to validate an input is alphanumeric or not using JavaScript?
March 15, 2026 - <html> <body> <h3>Using <i>charCodeAt() method</i> to validate alphanumeric strings</h3> <div id="output"></div> <script> var output = document.getElementById('output'); let str1 = "Hello123World"; let str2 = "Hello 123!"; function validateString(string) { for (let char of string) { let charCode = char.charCodeAt(0); if (!(charCode > 47 && charCode < 58) && !(charCode > 96 && charCode < 123) && !(charCode > 64 && charCode < 91)) { output.innerHTML += "'" + string + "' is NOT alphanumeric<br>"; return; } } output.innerHTML += "'" + string + "' is alphanumeric<br>"; } validateString(str1); validateString(str2); </script> </body> </html>
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › string › string is alpha or alphanumeric
Check if a JavaScript string contains only alpha or alphanumeric characters - 30 seconds of code
March 24, 2024 - const isAlphaNumeric = str => /^[a-z0-9]*$/gi.test(str); isAlphaNumeric('hello123'); // true isAlphaNumeric('123'); // true isAlphaNumeric('hello 123'); // false (space character is not alphanumeric) isAlphaNumeric('#$hello'); // false · 💡 Tip · These methods can serve as a great starting point for more complex string validation patterns. You can further customize the regular expressions to suit your specific requirements (e.g. allowing spaces, hyphens, or underscores in the string). JavaScript ·
🌐
Code2night
code2night.com › Blog › MyBlog › Alphanumeric-validation-in-JavaScript
Alphanumeric validation in JavaScript | Code2night.com
August 14, 2022 - <form> <div class="form-group row"> <label for="inputPassword" class="col-sm-2 col-form-label">Enter Only Alphanumeric</label> <div class="col-sm-10"> <input type="text" class="form-control" id="inputAlphanumeric" placeholder="Enter Only Alphanumeric" value="" name="inputAlphanumeric" onkeypress="return onlyAlphanumerics(event)" /> </div> </div> </form> Here is how to validate the input only to accept alphanumeric this will only take alphanumeric values like "abcc1234".
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-regexp-exercise-10.php
JavaScript validation with regular expression: Check whether a given value is alpha numeric or not - w3resource
... function is_alphaNumeric(str) { regexp = /^[A-Za-z0-9]+$/; if (regexp.test(str)) { return true; } else { return false; } } console.log(is_alphaNumeric("37828sad")); console.log(is_alphaNumeric("3243#$sew"));
🌐
RoseIndia
roseindia.net › javascript › AlphanumericValidationinJavaScript.shtml
Alphanumeric Validation in JavaScript
Alphanumeric validation means a field in the form can accept only numbers or characters other than that will not be accepted. In this type of validation you can input only the characters in the name field , number in the registration field, or a combination of number and characters in id field ...
🌐
Plain English
plainenglish.io › home › blog › javascript › check if string is alphanumeric in javascript
Check if string is Alphanumeric in JavaScript
December 31, 2022 - 3. You can also use the String.prototype.search() method to check if a string is alphanumeric. function isAlphanumeric(str) { return str.search(/^[a-zA-Z0-9]+$/) !== -1; } console.log(isAlphanumeric("abc123")); // true console.log(isAlphanu...
Find elsewhere
🌐
IT Explore
itexplore.org › homepage › tips › validating strings as alphanumeric using regex in javascript
Validating Strings as Alphanumeric Using Regex in JavaScript | IT Explore
April 27, 2025 - To validate alphanumeric characters using regular expressions, use ^[a-zA-Z0-9]+$. This pattern matches strings composed of one or more alphanumeric characters, such as "3DModel".
🌐
LabEx
labex.io › tutorials › string-is-alphanumeric-28407
Checking if a String is Alphanumeric | LabEx
Learn how to use JavaScript and regular expressions to determine if a given string contains only alphanumeric characters.
🌐
HScripts
hscripts.com › scripts › JavaScript › alphanumeric-check.php
FREE JavaSCRIPT - alphanumeric validation string, alpha numeric check using JS
<form name="test" onSubmit="return alphanumeric(test.mailf.value)"> <input name=mailf type=text> <input type=submit value=validate> </form> The alphanumeric() function is triggered on submission of the form. Just copy the javascript code and use it for free.
🌐
Coderanch
coderanch.com › t › 119775 › languages › alphanumeric-validation
alphanumeric validation (HTML Pages with CSS and JavaScript forum at Coderanch)
You learn the simple validation just for getting the some degree of idea about javascript without DOM and then proceed to handle with DOM. [ April 08, 2007: Message edited by: Prithiraj Sen Gupta ] ... you can try also using like this, function alphnumericValidate(event) { var val; if (navigator.appName == "Microsoft Internet Explorer") val = window.event.keyCode; else if (navigator.appName == "Mozilla") val = event.keyCode; if(val>=48 && val<=57) { var txt=obj.value; if(txt.length<=4) return true; else return false; } else if(val==8) return true; else return false; }
🌐
ASPSnippets
aspsnippets.com › Articles › 2686 › Perform-AlphaNumeric-validation-Alphabets-and-Numbers-using-OnKeyPress-in-JavaScript
Perform AlphaNumeric validation Alphabets and Numbers using OnKeyPress in JavaScript
January 29, 2019 - When User types in the TextBox, the text in the TextBox will be validated using OnKeyPress event handler in JavaScript and if the inputted character is not AlphaNumeric i.e. Alphabet or Number, the error message will be displayed next to the TextBox.
🌐
W3Resource
w3resource.com › javascript › form › letters-numbers-field.php
JavaScript : Checking for Numbers and Letters - w3resource
November 14, 2023 - You can write a JavaScript form validation script to check whether the required field(s) in the HTML form contains only letters and numbers. Javascript function to check if a field input contains letters and numbers only · // Function to check ...
🌐
Javascript-coder
javascript-coder.com › form-validation › javascript-validation-password-alphanumeric-string
Javascript password validation alphanumeric string | JavaScript Coder
In case you want to allow only alpha-numeric characters in your password, here is the validation to check that condition: Here is the function to test whether the inut matches an alphanumeric pattern:
🌐
Medium
medium.com › luisbajana › allowing-only-alphanumeric-characters-89ff03d9171
Allowing only alphanumeric characters | by Luis Bajaña | luisbajana | Medium
September 18, 2017 - Sometimes you have to include some extreme validations over your text fields, the other day I had this requirement: “Allowing only alphanumeric characters and validate it when the user is typing”, you may say “there are tons of libraries for that purpose out there”, yes, but, what if you just want to validate one field? Include an entire library for that is too much… · [code language=”javascript”] $(‘body’).on(‘keypress’,’.js-only-alpha-numeric’, function (e){ var re = new RegExp(“^[a-zA-Z0–9]+$”, “g”); if(!re.test(String.fromCharCode(e.keyCode))) return false; }); [/code]
🌐
TheLinuxCode
thelinuxcode.com › home › how i validate alphanumeric input in javascript (regex, edge cases, and production patterns)
How I Validate Alphanumeric Input in JavaScript (Regex, Edge Cases, and Production Patterns) – TheLinuxCode
February 4, 2026 - If you want the most reliable “alphanumeric” validation in JavaScript, I’d start with one decision: are you validating a machine-facing identifier or a human-facing name? For machine-facing values (promo codes, SKUs, short tokens), strict ASCII is the safest and most predictable rule.
🌐
LabEx
labex.io › tutorials › javascript-string-is-alpha-28408
Mastering String Alphanumeric Validation in JavaScript | LabEx
In this lab, we will be exploring ... We will use a regular expression pattern to test the input string and return a boolean value indicating whether the string contains only alphabetic characters or not....