Search code examples
phpformsvalidationnumeric

PHP form numeric validation


I have been trying to use PHP to validate my form.

The form asks users to enter details which will then get entered into a table in a database once the form has been validated.

I have a customer ID field in the form, and I am trying to validate it to make sure that it has a value (compulsory field), contains only numeric characters, is exactly 6 digits in length and is a unique ID (i.e. does not already exist in the database).

Here is what I have so far :

<body>

<?php
$temp = "";
$msg = "";
$rst = "";

if (isset($_POST["submit"])) {  
$number = $_POST["custid"];

if(empty($number)) {
$msg = '<span class="error"> Please enter a value</span>';
} else if(!is_numeric($number)) {
$msg = '<span class="error"> Data entered was not numeric</span>';
} else if(strlen($number) != 6) {
$msg = '<span class="error"> The number entered was not 6 digits long</span>';
} else {
echo "valid";
}

}

?>

<h1>Customer Information Collection <br /></h1>

<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="custinfo" >
<table>
<tr>
    <td><label for="custid">Customer ID (integer value): </label></td>
    <td><input type="text" id="custid" name="custid" value="<?php echo   $temp ?>" size=11 /><?php echo $msg; ?></td>
</tr>

<tr>
    <td><label for="customerfname">Customer Frist Name: </label></td>
    <td><input type="text" id="customerfname" name="fname" size=50/></td>
</tr>
<tr>
    <td><label for="customerlname">Customer Last Name: </label></td>
    <td><input type="text" id="customerlname" name="lname" size=50/></td>
</tr>
<tr>
    <td><label for="customeraddress">Customer Address: </label></td>
    <td><input type="text" id="customeraddress" name="custaddress" size=65/></td>

    <td><label for="suburb"> Suburb: </label></td>
<td><input type="text" id="suburb" name="suburb"/></td>
</tr>
<tr>
<td>
State:<select name="state" id="state">
    <option value="select">--</option>
    <option value="ACT">ACT</option>
    <option value="NSW">NSW</option>
    <option value="NT">NT</option>
    <option value="QLD">QLD</option>
    <option value="SA">SA</option>
    <option value="TAS">TAS</option>
    <option value="VIC">VIC</option>
     <option value="WA">WA</option>
  </select>
</td>
<td><label for="postcode"> Post Code: </label><input type="text" id="postcode" name="postcode" size=4/></td>
</tr>
</table>
<p><input type="submit" name="submit" value="Save Data"/>&nbsp;<input type="reset" value="Clear Form" />
</tr>

</form>

</body> 

The problem I am having is that when I purposely enter incorrect values into the customer ID field, it doesn't give me any error. It just processes the incorrect values as if they were correct.

Any help would be really great! If any more information is needed, just ask.


Solution

  • Here is your validation simplified, and with the correct operation to check the length of the id.

    if(empty($number)) {
        $msg = '<span class="error"> Please enter a value</span>';
    } else if(!is_numeric($number)) {
        $msg = '<span class="error"> Data entered was not numeric</span>';
    } else if(strlen($number) != 6) {
        $msg = '<span class="error"> The number entered was not 6 digits long</span>';
    } else {
        /* Success */
    }