Solved

Trying to validate a form with the number 0

Posted on 2009-04-06
3
148 Views
Last Modified: 2013-12-12
I have a form and a validation script to insert the data. I want to allow the user to enter the values 0,1,2, or 3. If a user enters 0 as a value, they get an error that nothing has been entered. How do i allow 0's to be entered?
Sample of the form 
 

<li>

              <label for="WoodFrog">Wood Frog</label>

              <input name="WoodFrog" type="text" value="<?php if(isset($_POST['WoodFrog'])); ?>" />

</li>
 
 

sample of the validation script
 

if(empty($_POST['WoodFrog']))

		{

			$errors[] = 'You forgot to enter the number of Wood Frogs.';

		}

		else 

		{

			if($_POST['WoodFrog'] == 0 OR $_POST['WoodFrog'] == 1 OR $_POST['WoodFrog'] == 2 OR $_POST['WoodFrog'] == 3)

			{

				$woodFrog = trim($_POST['WoodFrog']);

			}

			else

			{

				$errors[] = 'You entered an invalid number for Wood Frog';

			}

		}

Open in new window

0
Comment
Question by:toymachiner62
  • 2
3 Comments
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24084080
empty(0) returns true USE isset instead

empty() reference:
http://us2.php.net/empty
Sample of the form 

 

<li>

              <label for="WoodFrog">Wood Frog</label>

              <input name="WoodFrog" type="text" value="<?php if(isset($_POST['WoodFrog'])); ?>" />

</li>

 

 

sample of the validation script

 

if(isset($_POST['WoodFrog']))

                {

                        $errors[] = 'You forgot to enter the number of Wood Frogs.';

                }

                else 

                {

                        if($_POST['WoodFrog'] == 0 OR $_POST['WoodFrog'] == 1 OR $_POST['WoodFrog'] == 2 OR $_POST['WoodFrog'] == 3)

                        {

                                $woodFrog = trim($_POST['WoodFrog']);

                        }

                        else

                        {

                                $errors[] = 'You entered an invalid number for Wood Frog';

                        }

                }

Open in new window

0
 
LVL 19

Accepted Solution

by:
NerdsOfTech earned 250 total points
ID: 24084100
or better yet use is_null()

http://us2.php.net/manual/en/function.is-null.php
Sample of the form 

 

<li>

              <label for="WoodFrog">Wood Frog</label>

              <input name="WoodFrog" type="text" value="<?php if(isset($_POST['WoodFrog'])); ?>" />

</li>

 

 

sample of the validation script

 

if(is_null($_POST['WoodFrog']))

                {

                        $errors[] = 'You forgot to enter the number of Wood Frogs.';

                }

                else 

                {

                        if($_POST['WoodFrog'] == 0 OR $_POST['WoodFrog'] == 1 OR $_POST['WoodFrog'] == 2 OR $_POST['WoodFrog'] == 3)

                        {

                                $woodFrog = trim($_POST['WoodFrog']);

                        }

                        else

                        {

                                $errors[] = 'You entered an invalid number for Wood Frog';

                        }

                }

Open in new window

0
 
LVL 2

Author Closing Comment

by:toymachiner62
ID: 31567353
is_null() works great. Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
htacces issue 2 40
Router for PHP reqeusts 12 32
Phone Dialer 5 36
writting html or javascript in PHP 8 14
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 …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 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

19 Experts available now in Live!

Get 1:1 Help Now