Validating Strings as Alphanumeric Symbols Using Regex in JavaScript
To check if a string contains only alphanumeric symbols using regular expressions, use ^[ -~]+$
. This regular expression matches strings composed of one or more alphanumeric symbols, such as "Hello, world!". If you want to exclude spaces, modify the pattern to ^[!-~]+$
.
Additionally, if you want to match a fixed-length alphanumeric string, use the pattern ^[ -~]{n}$
. For example, ^[ -~]{6}$
matches a string exactly 6 characters long. You can also specify minimum and maximum lengths to create flexible patterns. For example, to match strings between 12 and 20 characters long, use ^[ -~]{12,20}$
.
Below are some commonly used regular expression patterns for validating alphanumeric symbols. Feel free to utilize them as needed.
# | Match Condition | Regular Expression Pattern |
---|---|---|
1 | Contains only alphanumeric symbols | ^[ -~]+$ |
2 | Fixed length of n alphanumeric symbols | ^[ -~]{n}$ |
3 | At least n alphanumeric symbols | ^[ -~]{n,}$ |
4 | No more than m alphanumeric symbols | ^[ -~]{1,m}$ |
5 | Between n and m alphanumeric symbols | ^[ -~]{n,m}$ |
Source Code
Below is a JavaScript function designed to check if the input string consists solely of alphanumeric characters and symbols. This function allows verification under the following conditions:
- If the minimum length is omitted: The string must consist only of alphanumeric symbols and not exceed the specified maximum length.
- If the maximum length is omitted: The string must consist only of alphanumeric symbols and meet the minimum length requirement.
- If both minimum and maximum lengths are omitted: The string must consist entirely of alphanumeric symbols.
/**
* Checks whether a given string consists only of alphanumeric characters and symbols.
*
* This function verifies that the string is composed of alphanumeric characters
* and symbols within the ASCII range (0x20 to 0x7E), 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 isAlphanumericSymbols(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(`^[ -~]{${min},}$`) // No limit on the maximum number of characters
: new RegExp(`^[ -~]{${min},${max}}$`);
// Perform the validation using regular expressions
return pattern.test(str);
}
Validation
Please specify the range in characters, not bytes.