[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

PHP FILTER_VALIDATE_INT

Posted on 2010-08-27
4
Medium Priority
?
564 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 111

Accepted Solution

by:
Ray Paseur earned 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

656 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