Validating Strings as Numeric Using Regex in PHP

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 ConditionRegular Expression Pattern
1All Half-width Digits^[0-9]+$
2Fixed-length n-digit Half-width Digits^[0-9]{n}$
3At least n-digit Half-width Digits^[0-9]{n,}$
4At most m-digit Half-width Digits^[0-9]{1,m}$
5Between n and m-digit Half-width Digits^[0-9]{n,m}$
Regex Pattern List and Conditions

Source Code

Next, we introduce a PHP 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.
 *
 * @param string $str The input string.
 * @param ?int $minLength Minimum length (defaults to 1 if null).
 * @param ?int $maxLength Maximum length (defaults to the string's length if null).
 * @return bool Returns true if the input string meets the conditions, false otherwise.
 * @throws InvalidArgumentException Throws an exception if $minLength is less than 1, or if $maxLength is less than $minLength.
 */
function isNumeric(string $str, ?int $minLength = null, ?int $maxLength = null): bool {
    // Set default values
    $min = $minLength ?? 1;
    $max = $maxLength ?? mb_strlen($str);

    // Validate arguments
    if ($min < 1) {
        // Throw an exception if minLength is less than 1
        throw new InvalidArgumentException('Minimum length must be an integer of 1 or more.');
    }

    if (!is_null($maxLength) && $max < $min) {
        // Throw an exception if maxLength is less than minLength
        throw new InvalidArgumentException('Minimum length must be less than or equal to maximum length.');
    }

    // Construct the half-width digit regex pattern
    $pattern = is_null($maxLength)
        ? sprintf('/^[0-9]{%d,}$/', (int)$min) // If maxLength is unlimited
        : sprintf('/^[0-9]{%d,%d}$/', (int)$min, (int)$max);

    // Perform the regex check
    return (bool)preg_match($pattern, $str);
}

Verification

Please specify the range in character count (number of characters), not bytes.

Argument Input

isNumeric("
",
,
);

Verification Result

Follow me!

photo by:Thought Catalog