Use
is_numeric($value);
return is true or false
Answer from Johannes Klauß on Stack OverflowUse
is_numeric($value);
return is true or false
If you want only numbers, remove the $custom part from the function. The /i implies case-insensitive matching, which is not relevant for numeric matches, and so can be removed.
private function numbers_only($value)
{
return preg_match('/^([0-9]*)
value);
}
The expression above will match zero or more numbers, so blank input is allowed. To require at least one number, change * to + as in
return preg_match('/^([0-9]+)
value);
And the [0-9]+ can be abbreviated as \d+. Since you are not capturing the value inside a an array of matches, there is no need for the extra overhead which is added by including the () capture group. That can be omitted as well.
return preg_match('/^\d+
value);
Or skip the regex entirely...
Finally, if you've gotten this far and are matching only integers, it is far easier and less resource-intensive to just do:
// If you really intend to match numbers only, and not all numeric values
// which might include .,
function numbers_only($value)
{
return ctype_digit(strval($value));
}
Simply you should change HTML input type
<input type="number" class="form-control" name="age" placeholder="Age">
if(isset($_POST['age']) && is_numeric($_POST['age']))
You can use type casting for this purpose:
_POST['age'];
In case the value isn't a number, it would be converted to one.
After that you should check if($age > 0) since a value which isn't a number probably would be converted to 0 (zero).
Furthermore, since it's an age value - you can also check for a range. Just for "convenience" validation.
A note regarding @AmanKumar & @OllyBarca answer/comment
While setting an input type="number" would force the user to enter a number. Any one with a little knowledge can bypass this one and send data directly to the server side. That's why you should NOT rely on client-side validation rules.
ctype_digit was built precisely for this purpose.
I use
if(is_numeric($value) && $value > 0 && $value == round($value, 0)){
to validate if a value is numeric, positive and integral
http://php.net/is_numeric
I don't really like ctype_digit as its not as readable as "is_numeric" and actually has less flaws when you really want to validate that a value is numeric.
Here is your validation simplified, and with the correct operation to check the length of the id.
if(empty($number)) {
$msg = '<span class="error"> Please enter a value</span>';
} else if(!is_numeric($number)) {
$msg = '<span class="error"> Data entered was not numeric</span>';
} else if(strlen($number) != 6) {
$msg = '<span class="error"> The number entered was not 6 digits long</span>';
} else {
/* Success */
}
In your case this is happening.
1) Checking empty string
2) Cheking numeric string and if it is numeric then checking the length.
Even after your validation fails for example if you enter invalid details, your validation captures the error and put it in $msg variable, but now your are not using that $msg variable if you echo that variable you can verify that it is working fine or not.
Hence do
echo $msg;
to verify your validation.
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
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.
You're missing [ before the A, and a ) before the final / in your regex. Should look like:
if (preg_match("/([A-Za-z0-9]+)/", $to)) {
Also, if you're only testing for boolean, and not collecting the matches, you could omit the () entirely.
Try:
if(preg_match("/([A-Za-z0-9])+/", $to)){
// action ...
}
You're missing [ at start and at the end ) in regexp
Can use
$validatedValue = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
See http://php.net/filter_input and related functions.
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.
}