473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Checking for blank data

I am working on form validation. I have a scheme that is working for
me, except for 3 input fields. Zip, Phone, and Fax are the input
fields. I want to do two tests. First I want to check to make sure
there is data actually in the box. Next, I want to make sure that data
is only numbers.

The scheme I am using is:

$Zip=stripslashes($_POST["Zip"]);

function check_zip($Zip)
{
if(!preg_match("/[^0-9]+$/ ",$Zip))
return TRUE;
else
return FALSE;
}

if(!check_zip($Zip))
{
$msg7="Please input a valid 5 digit zip code. (No letters or special
characters.)";
$error++;
}

if ($Zip="")
{
$msg7="Please input a valid 5 digit zip code. (No letters or special
characters.)";
$error++;
}

Granted, I just read up on pregmatch today so it is new to me. The
problem I am having is that it doesn't report that the box is empty. If
I type letters into the box, it returns the error message, asking for a
valid 5 digit zip code. So the first IF seems to be working. If I put
the second IF in front of the first (rearrange them) it still doesn't
work. I thought "" was the basic way to check for NULL data across most
languages. I tried looking it up on the PHP website but didn't find
anything to indicate a different syntax. I tried doing an IF/ELSE but
that didn't work either. Any help is appreciated.

Dec 7 '06 #1
10 1382

Jerim79 wrote:
I am working on form validation. I have a scheme that is working for
me, except for 3 input fields. Zip, Phone, and Fax are the input
fields. I want to do two tests. First I want to check to make sure
there is data actually in the box. Next, I want to make sure that data
is only numbers.

The scheme I am using is:

$Zip=stripslashes($_POST["Zip"]);

function check_zip($Zip)
{
if(!preg_match("/[^0-9]+$/ ",$Zip))
return TRUE;
else
return FALSE;
}

if(!check_zip($Zip))
{
$msg7="Please input a valid 5 digit zip code. (No letters or special
characters.)";
$error++;
}

if ($Zip="")
{
$msg7="Please input a valid 5 digit zip code. (No letters or special
characters.)";
$error++;
}

Granted, I just read up on pregmatch today so it is new to me. The
problem I am having is that it doesn't report that the box is empty. If
I type letters into the box, it returns the error message, asking for a
valid 5 digit zip code. So the first IF seems to be working. If I put
the second IF in front of the first (rearrange them) it still doesn't
work. I thought "" was the basic way to check for NULL data across most
languages. I tried looking it up on the PHP website but didn't find
anything to indicate a different syntax. I tried doing an IF/ELSE but
that didn't work either. Any help is appreciated.
Forgot to mention that I know you don't have to have two IF statements.
The first IF should return an error when it doesn't find numbers. But
that wasnt' working either.

Dec 7 '06 #2
Is empty() what your looking for.

You could also achieve it using strlen(), but empty is better.

Dec 7 '06 #3
function check_zip($Zip)
{
if(preg_match("/^[0-9]+$/ ",$Zip))
return TRUE;
else
return FALSE;

}
Also:
if(true == false){
echo("php is broken");
}

Note "==" instead of "=".

I reccomend reading this:
http://uk2.php.net/manual/en/language.operators.php

Hope this helps!
William

Dec 7 '06 #4
Untested!!!

$Zip=stripslashes($_POST["Zip"]);

if (empty($Zip)) {
$msg7="Please input a 5 digit zip code)";
$error++;
}

if(!preg_match("/^[0-9]+$/ ",$Zip)){
$msg7="Please input a valid 5 digit zip code. (No letters or special
characters.)";
$error++;
}

:)

Dec 7 '06 #5

SquishedOrange wrote:
Untested!!!

$Zip=stripslashes($_POST["Zip"]);

if (empty($Zip)) {
$msg7="Please input a 5 digit zip code)";
$error++;
}

if(!preg_match("/^[0-9]+$/ ",$Zip)){
$msg7="Please input a valid 5 digit zip code. (No letters or special
characters.)";
$error++;
}

:)
Shortly after posting this I was able to find empty. It works like a
charm. Thanks.

Dec 7 '06 #6

Jerim79 wrote:
SquishedOrange wrote:
Untested!!!

$Zip=stripslashes($_POST["Zip"]);

if (empty($Zip)) {
$msg7="Please input a 5 digit zip code)";
$error++;
}

if(!preg_match("/^[0-9]+$/ ",$Zip)){
$msg7="Please input a valid 5 digit zip code. (No letters or special
characters.)";
$error++;
}

:)

Shortly after posting this I was able to find empty. It works like a
charm. Thanks.
No problem. Note the change to your regular expression in the
preg_match line though.
Your old one won't work exactly as you intended and will match strings
that are only letters. Only by using a couple of "!" did it appear to
work correctly.

Dec 7 '06 #7
..oO(Jerim79)
>$Zip=stripslashes($_POST["Zip"]);
Why do you use this?

Micha
Dec 7 '06 #8
Jerim79 schrieb:
if ($Zip="")
Firstly, you need == there. The way it's now, the if statement will
/set/ $zip to "".

Secondly, PHP has "weak types". This means that any variable can be used
like a string, a number or a boolean, etc.

Therefore, this works:

if (!$zip) {
echo "zip is either '', 0, false or not set at all.";
}

Note that if PHP is set to display "notice"-level errors, it will
generate a warning if $zip was never set. You can avoid this by first
checking if $zip was ever defined:

if (!isset($zip) || !$zip) {
echo "zip is missing";
}

--
CB
Dec 8 '06 #9

Michael Fesser wrote:
.oO(Jerim79)
$Zip=stripslashes($_POST["Zip"]);

Why do you use this?

Micha
stripslashes() strips the any spaces off the input either from the
beginning or ending of the text. It is good for formatting reasons.

Dec 8 '06 #10
..oO(Jerim79)
>stripslashes() strips the any spaces off the input either from the
beginning or ending of the text. It is good for formatting reasons.
That's not what stripslashes() does. You're looking for trim().

Micha
Dec 8 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dariusz | last post by:
I have a small form that on button press submits to a PHP file for further processing into a database. I have to change the script as it allows blank / nothing to be submitted to the database. ...
2
by: shank | last post by:
I use ASP to check users data they submit. In particular, I'm checking for credit cards and I also use a server component DynuCreditCard. It basically affirms that the number "could be" a real...
5
by: Sue | last post by:
As soon as a character is entered in the first field of a new record, the number of records shown in the navigation buttons increases by 1 and the new record button becomes enabled. In the...
12
by: Vicky | last post by:
What is the better way of checking blank string is it strvar.lenght > 0 or strvar == "" or something else Thnaks
6
by: Juggler | last post by:
Hi all I've written a program in VB.NET that allows the user to build quotes for installing shower enclosures. As part of the program, I've included a blank Access database. I've provided them...
4
by: Patient Guy | last post by:
Does anyone have any coding rules they follow when doing argument checking? When arguments fail during check, do you return from the call with an ambiguous return value, or do you throw...
4
by: sparks | last post by:
I am trying to fix a database that someone did about 4 yrs ago in access97. The main table just contains demographics and is on the main form of the database. It has a subform on a tab that...
42
by: =?Utf-8?B?UGxheWE=?= | last post by:
I have an if statement that isn't working correctly and I was wondering how I check for a blank string. My Code Example if me.fieldname(arrayIndex) = "" then ----- end if When I do this and...
7
by: elnoire | last post by:
Greetings! I've just started learning python, so this is probably one of those obvious questions newbies ask. Is there any way in python to check if a text file is blank? What I've tried to...
4
by: barmatt80 | last post by:
I have created an unbound form so that when users enter the information, there is a save button they have to click, checking to make sure a field(txtDocketNo) is not blank. For some reason I cannot...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.