Validating Strings as Numeric Using Regex in JavaScript
To check if a string consists of half-width digits using regular expressions, use the pattern ^[0-9]+$
.
This regex means "a string composed of one or more digits from 0 to 9." It matches strings like "001," "9," and "12345." It will not match strings that contain characters other than half-width digits, such as "A1234."
Additionally, to match half-width digits of a specific length, use curly braces {}
. For example, to match a 7-digit half-width number, use ^[0-9]{7}$
, which is useful for validating postal codes. To match half-width digits between 5 and 8 characters long, you can write ^[0-9]{5,8}$
.
# | Match Condition | Regular Expression Pattern |
---|---|---|
1 | All Half-width Digits | ^[0-9]+$ |
2 | Fixed-length n-digit Half-width Digits | ^[0-9]{n}$ |
3 | At least n-digit Half-width Digits | ^[0-9]{n,}$ |
4 | At most m-digit Half-width Digits | ^[0-9]{1,m}$ |
5 | Between n and m-digit Half-width Digits | ^[0-9]{n,m}$ |
Source Code
Next, we introduce a JavaScript function that determines if an input string consists solely of half-width digits. This function can validate strings based on the following conditions:
- If `minLength` is omitted: Checks if the string consists only of half-width digits and does not exceed the specified maximum length.
- If `maxLength` is omitted: Checks if the string consists only of half-width digits and meets or exceeds the specified minimum length.
- If both `minLength` and `maxLength` are omitted: Checks if the entire string consists solely of half-width digits.
/**
* Checks if a string consists only of half-width digits.
*
* This function verifies if the string is composed entirely of half-width digits
* and falls within the specified length range.
*
* @param {string} str The input string.
* @param {?number} minLength Minimum length (defaults to 1 if null).
* @param {?number} maxLength Maximum length (defaults to the string's length if null).
* @return {boolean} Returns true if the input string meets the conditions, false otherwise.
* @throws {Error} Throws an error if $minLength is less than 1, or if $maxLength is less than $minLength.
*/
function isNumeric(str, minLength = null, maxLength = null) {
// Set default values
const min = minLength === null ? 1 : Number(minLength);
const max = maxLength === null ? str.length : Number(maxLength);
// Input type validation
if(isNaN(min)) {
throw new Error("Minimum length must be an integer.");
}
if(isNaN(max)) {
throw new Error("Maximum length must be an integer.");
}
// Argument validation
if (min < 1) {
// Throw an error if minLength is less than 1
throw new Error("Minimum length must be an integer of 1 or more.");
}
if (maxLength !== null && max < min) {
// Throw an error if maxLength is less than minLength
throw new Error("Minimum length must be less than or equal to maximum length.");
}
// Construct the half-width digit regex pattern
const pattern = maxLength === null
? new RegExp(`^[0-9]{${min},}$`) // If maxLength is unlimited
: new RegExp(`^[0-9]{${min},${max}}$`);
// Perform the regex check
return pattern.test(str);
}
Verification
Please specify the range in character count (number of characters), not bytes.