Hi all,

I am a newbie in PHP and is validating a form in PHP. I have an issue in this validation. The issue is that form validation is working properly to the field of email address. After this email comes the password and confirm password. The password fields are not being validated by the code and it sends the data to the database.

It should validate the password if not given, then it should check the confirm password field if empty or not and after this, the confirm password should match the password with the text or password given in the password field.

It is not doing as i have described above.

The validation code is given below! Please suggest where i am making a mistake!

<?php 

function validation ($data){
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
};
if (isset($_POST['submit']))
    {

        if (validation($_POST[fname]) == '')
        {
            $err = "Please enter your first name!<br />";
        }
        else if (validation($_POST[lname]) == '')
        {
                        $err = "Please enter last name!<br />";
        }
        else if (validation($_POST[mobile] == ''))
        {
            $err = "Please enter your mobile number!<br />";
        }
            else if(!ereg("^[0-9]{3}-[0-9]{3}-[0-9]{4}$", $_POST[mobile])) 
            { 
                   $err = 'Please enter your valid phone number';
            }
        else if(validation($_POST[email] == '')) 
        {   
            $err = "Please enter your email!<br />"; 
        }
        else if ($_POST[email])
        { 
            $email = $_POST[email]; 
            if(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { 
                    $err = 'Email invalid because wrong number of characters!<br />';
            }
        }
        else if(validation($_POST[password]) == '')
        { 
                         $err = "Please enter your password!";
        }  
        else if (validation($_POST[confirm]) == '')
        { 
                         $err = "Please confirm your password!";
        }  
        else if (validation($_POST[confirm]))
        {   
            $password = $_POST[password];
            $confirm = $_POST[confirm];
            if ($confirm != $password) {
            $err = "Your passwords does not match!<br />";
            }
        }
        else if($_POST[gender])
        { 
                        $err = "Please select your gender!";
        } 

        $msg = 'Data is fine and sent!';

        };

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="test.php">
<table width="586" border="0" cellspacing="0" cellpadding="0">
 <?php if ($err) {?>
  <tr>
    <td height="35" colspan="2" bgcolor="#FF0000"><font face="verdana" size="3"><strong><?php echo $err; ?></strong></font></td>
  </tr>
  <?php } else {?>
  <tr>
    <td height="35" colspan="2" bgcolor="#00FF00"><font face="verdana" size="3"><strong><?php echo $msg; ?></strong></font></td>
  </tr>
  <?php }; ?>
  <tr>
    <td height="35" colspan="2"><strong>CREATE ACCOUNT</strong>:-</td>
  </tr>
  <tr>
    <td width="165" height="29">First Name</td>
    <td width="389"><input type="text" name="fname" id="fname" /></td>
  </tr>
  <tr>
    <td height="30">Last Name</td>
    <td><input type="text" name="lname" id="lname" /></td>
  </tr>
  <tr>
    <td height="31">Mobile #</td>
    <td><input name="mobile" type="text" id="mobile" value="555-555-5555" />
     555-555-5555 </td>
  </tr>
  <tr>
    <td height="30">E-mail</td>
    <td><input type="text" name="email" id="email" /></td>
  </tr>
  <tr>
    <td height="29">Password</td>
    <td><input type="password" name="password" id="password" /></td>
  </tr>
  <tr>
    <td height="34">Re-type Password</td>
    <td><input type="password" name="confirm" id="confirm" /></td>
  </tr>
  <tr>
    <td height="34">I am</td>
    <td><select name="gender">
        <option >Select</option>
        <option value="male" >Male</option>
        <option value="female">Female</option>
      </select></td>
  </tr>
  <tr>
    <td height="32">&nbsp;</td>
    <td><label>
      <input type="checkbox" name="checkbox" id="checkbox" />
      </label>
      I agree to the terms and conditions hafeezcenter.pk</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td rowspan="2"><label>
      <input type="submit" name="submit" id="submit" value="Submit" />
    </label></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    </tr>
</table>
</form>
</body>
</html>

I'll be thankful to you!
Best Regards!
Bye.

Recommended Answers

All 3 Replies

The problem is that when you provide an email, then the following IS executed:

else if ($_POST[email])
{
	$email = $_POST[email]; 
	if(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
	{ 
		$err = 'Email invalid because wrong number of characters!<br />';
	}
}

In other words, it goes INTO the email else if clause and thus skips all the remaining else if clauses. Try using this instead:

else if( !ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email']) )
{
	$err = 'Email invalid because wrong number of characters!<br />';
}

yes, this resolved my issue.

Thanx for the help and your time!

You are welcome!

Regards,
Hielo

PS: Don't forget to mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.