Jump to content

text box, how to allow it to remain empty but also check email format


bubblybabs

Recommended Posts

Hello,

I have searched the net and this forum but can't seem to find what I need...

I would like to have multiple email text boxes that will allow the unused boxes remain blank but check that any entered emails are in the correct format...  Right now what I have causes an error because it's trying to force all of the boxes to have an entry...  what I'm using is:

function validEmail($address)
{
if (eregi("^[a-z0-9]([\._\-]?[a-z0-9])*@[a-z0-9]([\.\-]?[a-z0-9])*\.[a-z]{2,}$", $address))
return true;
else
return false;
}

How do I make it so that a box may be blank but also check the format if there is something entered?

Thank you,
Babs
Hehe someone beat me to it, but heres a farther explanation.

You could do
if(isset($var) && !validEmail($var)) {
//error
}

Example:

[code=php:0]
<?

function validEmail($address)
{
  if (eregi("^[a-z0-9]([\._\-]?[a-z0-9])*@[a-z0-9]([\.\-]?[a-z0-9])*\.[a-z]{2,}$", $address))
      return true;
  else
      return false;
}

if($_POST) {
$emails = $_POST['email'];
$i = 1;
foreach($emails as $k=> $v) {
if(strlen($v) > 0 && !validEmail($v)) {
$error .= "Email{$i} is invalid.<br>";
}
$i++;
}
}
if(!$_POST || $error) {
form($error);
}

function form($error = '') {
?>
<font color="red"><?=$error;?></font>
<form action="" method="post">
Email1: <input type="text" name="email[]"><br>
Email2: <input type="text" name="email[]"><br>
Email3: <input type="text" name="email[]"><br>
Email4: <input type="text" name="email[]"><br>
Email5: <input type="text" name="email[]"><br>
</input type="submit" value="Submit">
</form>
<?
}

?>
[/code]
Many thanks for trying to point me in the right direction...

This is what I tried:

if (isset($_POST['to_email1'])) $_SESSION['to_email1'] = $_POST['to_email1']; else $_SESSION['email1'] = '';

with the form validation that I have above...

I still get an error stating that it's not a valid email address...

What am I doing wrong?

Of note, I did try what you put up corbin but I got an error...

Thanks,
Babs

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.