Jump to content

Form Validation : Showing errors without passing inputs


ankur0101

Recommended Posts

Hello,

I am doing a php registration form, file name is register.php

<?php
include("config.php");

$submit = strip_tags($_POST['submit']);
$username = strip_tags($_POST['user_box']);
$password = md5(strip_tags($_POST['pass_box']));
$cpassword = md5(strip_tags($_POST['c_pass_box']));
$email = strip_tags($_POST['email_box']);
$mobile = $_POST['mobile_box'];
$ip = $_SERVER['REMOTE_ADDR'];
$date = date('Y-m-d');
$time = date('h-i-s');
$i = 0;

$checkusername = mysql_num_rows(mysql_query("SELECT * FROM members WHERE username='$username'"));
$checkemail = mysql_num_rows(mysql_query("SELECT * FROM members WHERE email='$email'"));

/* Validating username field */
if($username != NULL)
{
    if (strlen($username) > 15 || strlen($username) < 6)
        {
            echo "<p>Username must be in range of 6 to 15 Characters.</p>";
            }
    else
    {
        //check in DB
        if ($checkusername == 1)
        {
            echo "<p>Username already exist in database.</p>";            
            }
        else
        {
            $i++;
            }
        }
    }
else
{
    echo "<p>Username cannot be Blank</p>";
    }

/* ----------validating password field---------- */
if ($password != "d41d8cd98f00b204e9800998ecf8427e" || $cpassword != "d41d8cd98f00b204e9800998ecf8427e")
{
    if ($password == $cpassword)
    {
        if (strlen($password) > 16 && strlen($password) < 4)
        {
            echo "<p>password must be in range of 4 to 16 Characters.</p>";
        }
        else
        {
            $i++;
            }

    }
    else
    {
    echo "<p>Passwords do not match.</p>";
    }
}
else
{
    echo "<p>Password cannot be empty</p>";
    }
/* ----------Validating Passwords End---------- */

/* ----------Validating Email field Starts---------- */
if($email != NULL)
{
    if($checkemail == 1)
    {
        echo "<p>Email already exist.</p>";
        }
    else
    {
        $i++;
        }
    }
else
{
    echo "<p>Email field cannot be empty.</p>";
    }
/* ----------Validating Email fiend ends---------- */

/* ----------Validating Email field Starts---------- */
if($mobile != NULL)
{
    if(strlen($mobile) >10)
    {
        echo "<p>Mobile cannot be more than 10 digits long</p>";
        }
    else
    {
        $i++;
        }
    }
else
{
    echo "<p>Mobile field cannot be empty.</p>";
    }
/* ----------Validating Email fiend ends---------- */

if ($i == 4)
{
mysql_query("INSERT INTO members (username, password, email, mobile, ip, date, time) VALUES ('$username', '$cpassword', '$email', '$mobile', '$ip', '$date', '$time')");
echo "<p>Successful Registration Done !</p>";
}


?>

<!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="">
  <table width="576" height="229" border="0">
    <tr>
      <td width="139">Username :</td>
      <td colspan="2"><label for="user_box"></label>
      <input type="text" name="user_box" id="user_box" size="30" height="30" />
      (Between 6 to 15 Characters)</td>
    </tr>
    <tr>
      <td>Password :</td>
      <td colspan="2"><label for="pass_box"></label>
      <input type="password" name="pass_box" id="pass_box" size="30" height="30" /> 
      (Between 4 to 16 Characters)</td>
    </tr>
    <tr>
      <td>Confirm Password :</td>
      <td colspan="2"><label for="c_pass_box"></label>
      <input type="password" name="c_pass_box" id="c_pass_box" size="30" height="30" /></td>
    </tr>
    <tr>
      <td>Email Address :</td>
      <td colspan="2"><label for="email_box"></label>
      <input type="text" name="email_box" id="email_box" size="30" height="30" /></td>
    </tr>
    <tr>
      <td>Mobile No. :</td>
      <td colspan="2"><label for="mobile_box"></label>
      <input type="text" name="mobile_box" id="mobile_box" size="30" height="30" /> 
      (10 Characters)</td>
    </tr>
    <tr>
      <td> </td>
      <td width="171"><input type="submit" name="submit" id="submit" value="Submit" /></td>
      <td width="252"><input type="reset" name="button2" id="button2" value="Reset" /></td>
    </tr>
  </table>
</form>
</body>
</html>

 

When I load page, it shows following errors before

Username cannot be Blank

Password cannot be empty

Email field cannot be empty.

Mobile field cannot be empty.

 

 

What I want is on loading page i.e. before giving any inputs, it should not show any errors.

 

 

Need help

 

 

Link to comment
Share on other sites

As an example:

When you load the page $username will equal '' (empty string)

 

with your logic, $username != null, so the else is executed, ''

/* Validating username field */
if($username != NULL)
{
    /* Bunch of stuff in here*/
}
else
{
    echo "<p>Username cannot be Blank</p>";
}

 

I think what you want is something like:

 

/* Validating username field */
if (isset($_POST['submit'])){ // only execute inside if the form was submitted
if ($username != NULL)
{
    /* Bunch of stuff in here*/
}
else
{
    echo "<p>Username cannot be Blank</p>";
}

/* Remaining form validation logic here...*/
}

Link to comment
Share on other sites

$username = strip_tags($_POST['user_box']);

It's better to change it:

$username=isset( $_POST['user_box']) ? strip_tags($_POST['user_box']) : null;

 

And later you can check as you did it before:

if($username != NULL) ...
// or just 
if( $username ) ...

 

offtopic: how can I highlight the PHP code here?

Link to comment
Share on other sites

Hi,

The following method worked

/* Validating username field */
if (isset($_POST['submit'])){ // only execute inside if the form was submitted
if ($username != NULL)
{
    /* Bunch of stuff in here*/
}
else
{
    echo "<p>Username cannot be Blank</p>";
}

/* Remaining form validation logic here...*/
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.