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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

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

Post 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";
   }
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

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

Post 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"
Last edited by edawson003 on Thu Sep 17, 2009 12:34 am, edited 2 times in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

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

Post 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.)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

What impact does the 5 digit rule have on that? Does it count the decimal? Stuff after the decimal?
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

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

Post 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";
   }
 
Last edited by edawson003 on Fri Sep 18, 2009 4:23 am, edited 1 time in total.
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

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

Post 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])+$)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

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

Post 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";
   }  
 
 
Post Reply