Solved

PHP FILTER_VALIDATE_INT

Posted on 2010-08-27
4
536 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 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

21 Experts available now in Live!

Get 1:1 Help Now