View Full Version : valid different form fields

06-24-2006, 09:45 PM
greeting to all

i got a simple form which has 4 input boxes, and all need to be valided before submit. the Name box "not empty", the Phonenumber box "digits only", the email box "valid email format" and the Confirm email box "just make sure email address matching".

the code i'm using now can do the first three jobs, but the matching function not too good. the problems i'm having now is:

1. the exclamtion mark already there when first load the form.
2. confirm email only works when one of the Email box and Confirm Email box is empty and the other one IS NOT(i wrote the matching code myself, so not surprised;) ).

here's the code

// Create an empty array to hold the error messages.
$arrErrors = array();
if (!empty($_POST['submit'])) {
// Each time there's an error, add an error message to the error array
// using the field name as the key.
if ($_POST['name']=='')
$arrErrors['name'] = 'Please enter your name.';
if (!preg_match("/^[0-9\. ]+$/",$_POST['phonenumber']))
$arrErrors['phonenumber'] = 'digits only! ';
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email']))
$arrErrors['email'] = 'Email address not valid!';
if (!$_POST['confirm']==$_POST['email'])
$arrErrors['confirm'] = 'Email address not matching!';

<form method="post" action="<?php echo $PHP_SELF; ?>">

<?php if (!empty($arrErrors['name'])) ?>
<label for="title">Name</label>
<input type="text" name="name" value="<?php echo $_POST['name'] ?>">
<?php if (!empty($arrErrors['name'])) echo '<font color="red">!</font>'.$arrErrors['name'] ; ?>

<?php if (!preg_match("/^[0-9\. ]+$/",$_POST['phonenumber'])) ?>
<label for="phonenumber">Phonenumber</label>
<input type="text" name="phonenumber" class"textfield" maxlength=5 value="<?php echo $_POST['phonenumber'] ?>">
<?php if (!preg_match("/^[0-9\. ]+$/",$_POST['phonenumber'])) echo '<font color="red">!</font>'.$arrErrors['phonenumber'] ; ?>

<?php if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) ?>
<label for="email">Email</label>
<input type="text" name="email" class"textfield" size="30" maxlength=40 value="<?php echo $_POST['email'] ?>">
<?php if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) echo '<font color="red">!</font>'.$arrErrors['email'] ; ?>

<?php if (!$_POST['confirm']==$_POST['email']) ?>
<label for="confirm">Confirm Email</label>
<input type="text" name="confirm" class"textfield" size="30" maxlength=40 value="<?php echo $_POST['confirm'] ?>">
<?php if (!$_POST['confirm']==$_POST['email']) echo '<font color="red">!</font>'.$arrErrors['confirm'] ; ?>


<div id="button">
<input name="submit" type="submit" class="submit" value="Submit" />


so, what's the problem? shall i use isDifferent function for the matching?(not try yet).
one more thing, the action direct the page back to itself, so how can i carry on insert the data into database when pass the validation?

appreciate any reply!

06-24-2006, 10:00 PM

if ($_POST['confirm'] != $_POST['email'])

06-24-2006, 10:06 PM
thx Fumigator, that part works beautifully now!
any idea about other problems?

07-27-2006, 02:34 PM
hello, can i just ask, which part of the code makes sure that the e-mail ad entered is valid e-mail format?

thanks! :)

07-27-2006, 02:36 PM
and where do i insert it in the code? thanks again! :)