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

Posted on 2004-11-28
Last Modified: 2006-11-17
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>";

The script will throw an error even if the width that is posted is less than 2000?
Question by:fdehell
    LVL 48

    Accepted Solution

    us this regular expression:

    why using a regular expression for this?
    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>";

    A regular expression woul look like this (untested)

    preg_match('/^1?[0-9]{0,3}|2000$/', $_POST['width'])
    LVL 1

    Author Comment

    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!!!
    LVL 4

    Expert Comment

    Hallo fdehell, it may help you ..

    if(is_numeric($w) && $w>1 && $w<2000)
    //do you what you want...
    //error in input...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    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…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to count occurrences of each item in an array.

    759 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

    10 Experts available now in Live!

    Get 1:1 Help Now