Solved

PHP FILTER_VALIDATE_INT

Posted on 2010-08-27
4
556 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

691 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