Use

is_numeric($value);

return is true or false

Answer from Johannes Klauß on Stack Overflow
🌐
PHP
php.net › manual › en › function.is-numeric.php
PHP: is_numeric - Manual
For example: <?php $v = is_numeric ('58635272821786587286382824657568871098287278276543219876543') ? true : false; var_dump ($v); ?> The above script will output: bool(true) So this function is not intimidated by super-big numbers. I hope this helps someone. PS: Also note that if you write is_numeric (45thg), this will generate a parse error (since the parameter is not enclosed between apostrophes or double quotes). Keep this in mind when you use this function. ... for strings, it return true only if float number has a dot is_numeric( '42.1' )//true is_numeric( '42,1' )//false
🌐
UI Bakery
uibakery.io › regex-library › numbers-only-regex-php
Numbers only regex (digits only) PHP
// Validate if a string is a valid real number $number_validation_regex = "/^(?:-(?:[1-9](?:\\d{0,2}(?:,\\d{3})+|\\d*))|(?:0|(?:[1-9](?:\\d{0,2}(?:,\\d{3})+|\\d*))))(?:.\\d+|)$/"; echo preg_match($number_validation_regex, '121220.22'); // returns 1 // Extract real number from a string $extract_number_pattern = "/(?:-(?:[1-9](?:\\d{0,2}(?:,\\d{3})+|\\d*))|(?:0|(?:[1-9](?:\\d{0,2}(?:,\\d{3})+|\\d*))))(?:.\\d+|)/"; $string_to_match = 'Pi equals to 3.14'; preg_match_all($extract_number_pattern, $string_to_match, $matches); print_r($matches[0])// matches[0] is ['3.14']
🌐
Learning About Electronics
learningaboutelectronics.com › Articles › How-to-perform-number-validation-in-PHP.php
How to Perform Number Validation in PHP
The is_numeric() function checks to see if the characters entered into a field are numbers or not. If all the characters are numbers, it returns true. If all the characters are not numeric, it returns false. Here we simply do an if-else statement. If all the characters are numbers, it tells ...
🌐
W3Schools
w3schools.com › php › func_var_is_numeric.asp
PHP is_numeric() Function
PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete
🌐
PHP ISSET
phpisset.com › php-validate-phone-number
Validating a Phone Number in PHP - PHP ISSET
June 19, 2020 - We use the PHP preg_match function to validate the given telephone number using the regular expression: ... This regular expression checks that the string $s parameter only contains digits [0-9] and has a minimum length $minDigits and a maximum length $maxDigits.
Find elsewhere
🌐
The Electric Toolbox
electrictoolbox.com › home › validating numbers with php
Validating numbers with PHP | The Electric Toolbox Blog
January 2, 2020 - The other alternative is the ctype_digit() function which saves having to mess around with regular expressions. ctype_digit checks if all of the characters in the passed in value are numerical. Only the numbers 0 to 9 are valid, and it doesn’t matter if the value is type cast as a string, ...
🌐
Stack Overflow
stackoverflow.com › questions › 22116247 › how-to-validate-a-text-input-for-only-numbers-using-php
regex - How to validate a text input for only numbers using PHP - Stack Overflow
@AmalMurali \d matches [0-9] and other digits like Eastern Arabic numerals. Since the question is about only digits from 0 to 9, it is more appropriate here to have [0-9]
🌐
AbstractAPI
abstractapi.com › api guides, tips & tricks › phone number validation in php
Phone Number Validation in PHP | Abstract
June 10, 2026 - Install the official package with composer require abstractapi/php-phone-validation, then call AbstractPhoneValidation::configure() with your API key and AbstractPhoneValidation::verify() with the phone number.
🌐
W3Schools
w3schools.com › php › filter_validate_int.asp
PHP FILTER_VALIDATE_INT Filter
FILTER_VALIDATE_INT also allows us to specify a range for the integer variable. ... Note: When specifying options in an array. The options must be in an associative multidimensional array with the name "options". Check if a variable is both of type INT, and between 1 and 200: <?php $int = 122; $min = 1; $max = 200; if (filter_var($int, FILTER_VALIDATE_INT, array("options" => array("min_range"=>$min, "max_range"=>$max))) === false) { echo("Variable value is not within the legal range"); } else { echo("Variable value is within the legal range"); } ?> Try it Yourself »
🌐
Stack Overflow
stackoverflow.com › questions › 32234140 › how-to-validate-a-input-to-numbers-only
php - How to validate a input to numbers only - Stack Overflow
The first section in the if-statement checks if the input is an integer, and the second section checks if the first number is 6, 7 or 8. <?php if(filter_var($_POST['txtNumero'], FILTER_VALIDATE_INT) AND in_array(substr($_POST['txtNumero'], 0, 1), array(6, 7, 8))) { echo "Correct"; } else { echo "Incorrect"; } ?>
🌐
David Walsh
davidwalsh.name › php-validatie-numeric-digits
PHP: Validating Numeric Values and Digits
March 12, 2010 - I've inherited a lot of code where the previous developer used PHP's is_numeric() and it's gotten them into trouble. There's a big difference between validating that a value is numeric and validating that a value is all digits. Here's how to validate the two: /* numeric, decimal passes */ function validate_numeric($variable) { return is_numeric($variable); } /* digits only, no dots */ function is_digits($element) { return !preg_match ("/[^0-9]/", $element); } A customer number, for example, should not have any decimal points but using is_numeric() would let a decimal value pass the test.
🌐
ThoughtCo
thoughtco.com › isnumeric-php-function-2694075
Here's How to Use "Is_numeric()" Functions in PHP
May 24, 2019 - <?php if (is_numeric("cake")) { echo "Yes"; } else { echo "No"; } ?> Because cake is not a number, this echos No. A similar function, ctype-digit(), also checks for numeric characters, but only for digits—no optional signs, decimals, or exponents allowed. Every character in the string text must be a decimal digit for the return to be true.
Top answer
1 of 2
2

Ignoring non-digits is the first step. The leading 1 is not required by all telephone companies, particularly near New Jersey. Dialing it there causes an error or a wrong number.

The area code cannot be [2-9]11 nor [2-9]9[0-9]. Area codes with a 9 as the center digit are reserved for an as-yet-undecided scheme to address area code exhaustion.

Exchanges also cannot begin with a 0 or 1, nor can they be [2-9]11.


These restrictions are expressed with this code:

$mobile = preg_replace ('/\D/', '', $trimmed['mobile']);

if ($mobile[0] == '1') $mobile = substr ($mobile, 1);  // remove prefix

$invalid = strlen ($mobile) != 10  ||
           preg_match ('/^1/',      $mobile) ||  // ac start with 1
           preg_match ('/^.11/',    $mobile) ||  // telco services
           preg_match ('/^...1/',   $mobile) ||  // exchange start with 1
           preg_match ('/^....11/', $mobile) ||  // exchange services
           preg_match ('/^.9/',     $mobile);    // ac center digit 9
2 of 2
0

After learning what I could from searching and scouring, I could not find a U.S. only method. So, here is what I devised:

First, to allow for any input format, use preg_replace to remove all non-digit characters. [Users can use whatever hyphens or slashes they want, but they are of no use to me.]

$mobile = preg_replace('/\D/', '', $trimmed['mobile']);

With just a pure string of digits to work with, eliminate any leading 1s — this takes care of any instance where the user included the leading 1 or where they started the area code with 1. [If valid, this should always leave me with a 10 digit string.]

$mobiletrim = ltrim($mobile, '1');

Next, regex checks that the trimmed phone number is precisely 10 digits long and does not begin with either a 0 or a 1.

    if (preg_match ('/^[2-9]\d{9}$/', $mobiletrim) || empty($trimmed['mobile'])) {
        $mp = TRUE;
        if (!empty($trimmed['mobile'])) { $mobiletrim = "1" . $mobiletrim; }
    } else {
        $profile_errors[] = "Please enter a valid U.S. phone number.";
    }
if ($mp) { //Store Phone Number in db }

The final step, which you can see above, was to add the leading 1 before storing the valid number in the db, provided that the user did not submit an empty phone field, which they would do if they didn't want their number stored.


Technically, this will validate all NANP numbers. The only way to narrow it down from NANP to U.S. exclusively would be to verify that a U.S. area code was used by parsing through a list of current U.S. area codes.