Validating Strings as Alphanumeric Using Regex in JavaScript
To validate alphanumeric characters using regular expressions, use ^[a-zA-Z0-9]+$
. This regular expression matches strings composed of one or more alphanumeric characters, such as "3DModel".
If you want to specify a fixed length of alphanumeric characters, use ^[a-zA-Z0-9]{n}$
. For example, to match exactly 6 characters, use ^[a-zA-Z0-9]{6}$
. Similarly, you can create flexible patterns by specifying minimum and maximum lengths. For instance, ^[a-zA-Z0-9]{12,20}$
is effective for matching strings between 12 and 20 characters.
Below is a summary of commonly used regular expression patterns. Feel free to use them as needed.
# | Match Condition | Regular Expression Pattern |
---|---|---|
1 | Contains only alphanumeric characters | ^[a-zA-Z0-9]+$ |
2 | Fixed length of n alphanumeric characters | ^[a-zA-Z0-9]{n}$ |
3 | At least n alphanumeric characters | ^[a-zA-Z0-9]{n,}$ |
4 | No more than m alphanumeric characters | ^[a-zA-Z0-9]{1,m}$ |
5 | Between n and m alphanumeric characters | ^[a-zA-Z0-9]{n,m}$ |
Source Code
Below is a JavaScript function to validate if a given string consists only of alphanumeric characters. The function allows validation based on the following conditions:
- If the minimum length is omitted: Validates that the string is within the specified maximum length and consists only of alphanumeric characters.
- If the maximum length is omitted: Validates that the string meets the minimum length requirement and consists only of alphanumeric characters.
- If both minimum and maximum lengths are omitted: Validates that the string entirely consists of alphanumeric characters.
/**
* Checks whether a given string consists only of alphanumeric characters.
*
* This function verifies that the string is composed of alphanumeric characters
* and that its length falls within the specified range.
*
* @param {string} str The input string.
* @param {?number} minLength The minimum number of characters (treated as 1 if null).
* @param {?number} maxLength The maximum number of characters (treated as the length of the string if null).
* @return {boolean} Returns true if the input string meets the conditions; otherwise, returns false.
* @throws {Error} Throws an error if the minimum length is less than 1,
* or if the maximum length is less than the minimum length.
*/
function isAlphanumeric(str, minLength = null, maxLength = null) {
// Set default values
const min = minLength === null ? 1 : Number(minLength);
const max = maxLength === null ? str.length : Number(maxLength);
// Type checks for input values
if (isNaN(min)) {
throw new Error("The minimum length must be an integer.");
}
if (isNaN(max)) {
throw new Error("The maximum length must be an integer.");
}
// Validate arguments
if (min < 1) {
// Throw an error if the minimum length is less than 1
throw new Error("The minimum length must be an integer greater than or equal to 1.");
}
if (maxLength !== null && max < min) {
// Throw an error if the maximum length is less than the minimum length
throw new Error("The minimum length must not exceed the maximum length.");
}
// Construct the regular expression pattern for alphanumeric characters and symbols
const pattern = maxLength === null
? new RegExp(`^[a-zA-Z0-9]{${min},}$`) // No limit on the maximum number of characters
: new RegExp(`^[a-zA-Z0-9]{${min},${max}}$`);
// Perform the validation using regular expressions
return pattern.test(str);
}
Validation
Specify the range in terms of characters (not bytes).