php validate integer
The manual says:
To test if a variable is a number or a
numeric string (such as form input,
which is always a string), you must
use is_numeric().
Alternative you can use the regex based test as:
if(preg_match('/^\d+$/',$_GET['id'])) {
// valid input.
} else {
// invalid input.
}
How to validate integer in php
You define an empty variable and if the post submits you check if it's empty. This will always be true because this variable has an empty string as value. You should try to use $_POST
. You should also change $_GET['id']
into $_POST['id']
:
<?php
$idErr = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$id = $_POST['id'];
if (empty($id)){
$idErr = "Student ID is required";
} elseif(preg_match('/^\d+$/',$_POST['id'])) {
$idErr = "Only numbers allowed";
} elseif(strlen($id) != 4) {
$idErr = "Please enter 4 numbers";
}
}
?>
You should be able to get a different result if you try this.
How can i validate integer form in php?
Try using this is you want to check it on runtime.
<tr><td>Phone: </td><td><input type="number" class="form-control" name="phone"/></td></tr>
Or use is_int
or is_numeric
if you want to check the phone number after POST.
filter_var and validating integer values
I think your problem lies more with integer:
The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18, except for Windows, which is always 32 bit. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.
FILTER_VALIDATE_INT
allows for min_range and max_range to be passed as options. You should probably use those.
Validating array only integer number in PHP
You can loop through the array.
foreach ($total as $num) {
if (!preg_match("/^[0-9]*$/", $num) ) {
$ErrMsg = "Only numeric value is allowed.";
echo $ErrMsg;
break;
}
}
Note also that your regular expression matches an empty string and treats it as an integer. If you don't want to include that, change *
to +
.
PHP filter validate int issue when first character is 0
There is nothing you can do to make this validation work. In any case, you should not be using FILTER_VALIDATE_INT
because telephone numbers are not integers; they are strings of digits.
If you want to make sure that $tel
is a string consisting of exactly 10 digits you can use a regular expression:
if (preg_match('/^\d{10}$/', $tel)) // it's valid
or (perhaps better) some oldschool string functions:
if (strlen($tel) == 10 && ctype_digit($tel)) // it's valid
Related Topics
Regexp in Preg_Match Function Returning Browser Error
When Should I Declare Variables in a PHP Class
Why and How to Use Anonymous Functions in PHP
Best Way to Process Large Xml in PHP
Download Files from Server PHP
Failed to Open Stream: Http Wrapper Does Not Support Writeable Connections
How to Check If $_Get Is Empty
Ssl Error Ssl3_Get_Server_Certificate:Certificate Verify Failed
Codeigniter: Load Controller Within Controller
PHP Substr() Function with Utf-8 Leaves � Marks at the End
A Yaml File Cannot Contain Tabs as Indentation
How to Get Unique Value in Multidimensional Array
Detect Clients with Proxy Servers via PHP
How to Return Back Twice in Laravel
Set Session Variable Using JavaScript in PHP
How to Force Users to Access My Page Over Https Instead of Http