Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form validation problem

Status
Not open for further replies.

gokeeffe

Programmer
Jan 11, 2005
170
0
0
IE
What I want to do is the following

I have a form with 8 fields,all of these fields are optional

but if one of the fields is filled in ie. the email address field

I want to validate the email address.

Hence I use this code


// Check for a email address
if (eregi ("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$",stripslashes(trim($_POST['email_address']))))
{
$e = escape_data($_POST['email_address']);
}
else
{
$e = FALSE;
echo '<p class = "validation">Please enter a valid email address</p>';
}


But what do I do if it is not filled in I don't want this code to happen so I do the following


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

{

// Check for a email address
if (eregi ("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$",stripslashes(trim($_POST['email_address']))))
{
$e = escape_data($_POST['email_address']);
}
else
{
$e = FALSE;
echo '<p class = "validation">Please enter a valid email address</p>';
}

}
else

{

carry on as normal


}

but this doesn't work it keeps going into the vaidation code even if it is empty

I have also used


if (strlen($_POST['email_address']) > 0)

{

etc, etc

but that doen't work either

Please help

Regards

Graham
 
try this:
POST['email_address']!=""

instead of using isset (if there is a textfield in the previous page it will return atleast an empty value. therefore isset will always be true!!!)

Known is handfull, Unknown is worldfull
 
I tryed this but it still doesn't work

if ($_POST['email_address']!="")
{
// email has been entered
// validate email address

if (eregi ("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$",stripslashes(trim($_POST['email_address']))))
{
$e = escape_data($_POST['email_address']);
}
else
{
$e = FALSE;
echo '<p class = "validation">Please enter a valid email address</p>';
}
}

is there something wrong with my input box

<tr>
<td colspan="2" class="labelcell"><label for = "additional_number">Email Address:</label></td>
<td colspan="2" class="fieldcell2"><input type="text" name="email_address" size="30" maxlength="30"
value="<?php if(isset($_POST['email_address'])) echo stripslashes ($_POST['email_address']);?>"/>
</td>
</tr>
 
try this:
echo($_POST['email_address']);

Known is handfull, Unknown is worldfull
 
Another note:
I would recommend to use the PCRE regular expression functions rather than POSIX. preg_match() would be my choice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top