Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

Problem with validating a numeric value which may only be between 1 and 2000 and not empty

Hi all

Im further working out an example i got from Gruntar moments ago, he showed me how to validate strings, and so i was working with his example to check for a numeric value which may only contain numbers and should be between 1 and 2000, anyway the following example is the one i cant get to work:(


if(!eregi("^[1-2000]{1,}$", $_POST['width'])) {
 print "<center><font class=\"text\">Field width: Enter the width between 1 and 2000 pixels, numerical values only</font></center>";
 exit;
}

The script will throw an error even if the width that is posted is less than 2000?
0
fdehell
Asked:
fdehell
1 Solution
 
hernst42Commented:
us this regular expression:

why using a regular expression for this?
Try
if ($_POST['width']<1 || $_POST['width']>2000) {
 print "<center><font class=\"text\">Field width: Enter the width between 1 and 2000 pixels, numerical values only</font></center>";
 exit;
}

A regular expression woul look like this (untested)

preg_match('/^1?[0-9]{0,3}|2000$/', $_POST['width'])
0
 
fdehellAuthor Commented:
Well hernst42, i did not know there was another way of doing this, that is the only reason i tried to use reg expr, but yours works just as fine, thx heaps!!!
0
 
ramu_src2kCommented:
Hallo fdehell, it may help you ..

$w=$_POST['width']
if(is_numeric($w) && $w>1 && $w<2000)
{
//do you what you want...
}
else
{
//error in input...
}
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now