Page 1 of 1

Using preg_match("/^[0-9]{1,5} to accept "0"

Posted: Sun Sep 13, 2009 8:06 pm
by edawson003
Using the below field validation code to require number entry; however, the code consider "0" and invalid entry. What would I need to tweak to accept "0" as a opposed to NULL (no field entry)?

Code: Select all

 
    if(!preg_match("/^[0-9]{1,5}$/", $_POST['sugar'])  && $_POST['sugar']) {
   $errors[] = "Yes";
   $sugarentryerror = "\t<br><span class=pred>Invalid value entered. Must me a numeric value less than 5 digits or leave blank.</span>\n";
   }
 

Re: Using preg_match("/^[0-9]{1,5} to accept "0"

Posted: Sun Sep 13, 2009 8:35 pm
by requinix
The expression does accept zero. It's the

Code: Select all

&& $_POST['sugar']
that does not.

Get rid of that and use {0,5} in the regex.

Re: Using preg_match("/^[0-9]{1,5} to accept "0"

Posted: Sun Sep 13, 2009 8:48 pm
by edawson003
Ok, set it up this way:

Code: Select all

 
      if(!preg_match("/^[0-9][color=#FF0000]{0,5}[/color]$/", $_POST['sugar'])) {
   $errors[] = "Yes";
   $sugarentryerror = "\t<br><span class=pred>Invalid value entered. Must me a numeric value less than 5 digits or leave blank.</span>\n";
   }
 
Still seems to be blocking "0"

Re: Using preg_match("/^[0-9]{1,5} to accept "0"

Posted: Mon Sep 14, 2009 2:59 am
by requinix

Code: Select all

var_dump(!preg_match("/^[0-9]{0,5}$/", "0"));

Code: Select all

bool(false)
And the if block does not execute.

The input you have is not just "0". Make sure you know exactly what's going through your form.

Re: Using preg_match("/^[0-9]{1,5} to accept "0"

Posted: Fri Sep 18, 2009 12:58 am
by edawson003
Thanks. I figured out how to get "0" through. Your assistance was helpful. Any thoughts on how to allow decimal numbers (i.e. 0.75, .25 etc.)

Re: Using preg_match("/^[0-9]{1,5} to accept "0"

Posted: Fri Sep 18, 2009 1:54 am
by requinix
What impact does the 5 digit rule have on that? Does it count the decimal? Stuff after the decimal?

Re: Using preg_match("/^[0-9]{1,5} to accept "0"

Posted: Fri Sep 18, 2009 2:41 am
by edawson003
Well, I use it for a form field validation and it rejects it altogether. I don't think it counts it at all.

Code: Select all

 
if(!$_POST['servingsize']){
   $reqerrors[] = "Yes";
   $servingsizeerror = "\t<br><span class=pred>You must enter amount of servings.</span>\n";
        }       
   elseif(!preg_match("/^[0-9]{1,2}$/", $_POST['servingsize']) && $_POST['servingsize']) {
   $reqverrors[] = "Yes";
   $servingsizeerror = "\t<br><span class=pred>Invalid value entered. Must be a numeric value less than 2 digits.</span>\n";
   }
 

Re: Using preg_match("/^[0-9]{1,5} to accept "0"

Posted: Fri Sep 18, 2009 2:46 am
by edawson003
I found the solution online:

Code: Select all

 
if (preg_match("/^-?([0-9])+\.?([0-9])+$/", $string)) 
 
1. Looks for an optional minus sign at the beginning of the string (^-?)
2. Looks for one or more number (([0-9])+)
3. Looks for an optional decimal point (\.?)
4. Looks for one or more numbers at the end of the string (([0-9])+$)

Re: Using preg_match("/^[0-9]{1,5} to accept "0"

Posted: Fri Sep 18, 2009 3:22 am
by VladSun

Re: Using preg_match("/^[0-9]{1,5} to accept "0"

Posted: Fri Sep 18, 2009 3:36 am
by requinix
VladSun wrote::mrgreen:

is_numeric()

:twisted:
That's what I was thinking, plus a < and >= in the condition, like

Code: Select all

if (is_numeric($value) && $value >= 0 && $value <= 100000)
but the 0/100000 might not be the right values... depending on the answer to my question.

Re: Using preg_match("/^[0-9]{1,5} to accept "0"

Posted: Fri Sep 18, 2009 11:12 am
by edawson003
That works! Thanks! 8O

How I apply it for field were I want whole numbers & decimal numbers, but no '0's aka zeros.

Code: Select all

 
 
if(!$_POST['servingsize']){
   $reqerrors[] = "Yes";
   $servingsizeerror = "\t<br><span class=pred>You must enter amount of servings.</span>\n";
        }    
   elseif(is_numeric($_POST['servingsize']) && $_POST['servingsize'] > 0) {
   }
   else {
   $reqverrors[] = "Yes";
   $servingsizeerror = "\t<br><span class=pred>Invalid value entered.</span>\n";
   }