Solved

PHP FILTER_VALIDATE_INT

Posted on 2010-08-27
4
540 Views
Last Modified: 2013-12-13
hi,

How would I validate a users integer input with this function?
I want the min number to be 0 and max to be 9.

and what if I have multiple user values that I want to validate do I put it in an array?


thanks!
0
Comment
Question by:Solutionabc
  • 2
4 Comments
 
LVL 11

Expert Comment

by:Rajesh Dalmia
ID: 33541503
can try something like....

<?php

$intVal = $_REQUEST['FormField'];

if($intVal > 0 and $intVal < 9)
{
echo "valid no.";
}
else
{
echo "invalid";
}

?>
0
 

Author Comment

by:Solutionabc
ID: 33541565
If I have the code below, and verseQ = 10, will var_dump return false?

if it does how do I check the returned value?
$verseQ = $_POST["verseQb"];

$int_verseQ = array("options"=>
array("min_range"=>0, "max_range"=>9));

var_dump(filter_var($verseQ, FILTER_VALIDATE_INT, $int_verseQ));

Open in new window

0
 
LVL 11

Expert Comment

by:Rajesh Dalmia
ID: 33541590
yes...
but if code not working then have to check what values is getting assigned to verseQ.

to make sure it always have integer value change the line to

$verseQ = $_POST["verseQb"] + 0;
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 33549494
Consider adding this to the top of all your scripts:

error_reporting(E_ALL);

It will reveal a multitude of important notices!

Also, you need to understand that PHP is a loosely typed language.  So in many cases, "10" (a string of two characters) will be treated the same as 10 (an integer equal to hexadecimal A).

As you expect, the code snippet show boolean FALSE.

Cheers, ~Ray
<?php // RAY_temp_Solutionabc.php
error_reporting(E_ALL);

// SET HERE TO AVOID THROWING A NOTICE
$_POST["verseQb"] = '10';

// COPIED FROM THE POST AT EE
$verseQ = $_POST["verseQb"];

$int_verseQ = array("options"=>
array("min_range"=>0, "max_range"=>9));

var_dump(filter_var($verseQ, FILTER_VALIDATE_INT, $int_verseQ));

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
change password links 7 72
PHP_POST() error message 9 40
<? versus <?php 5 36
Before I get too far.. best way to save dates data 4 14
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now