title: PHP form numeric validation
body: <p>I have been trying to use PHP to validate my form.</p>
  
  <p>The form asks users to enter details which will then get entered into a table in a database once the form has been validated.</p>
  
  <p>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).</p>
  
  <p>Here is what I have so far :</p>
  
  <pre><code>&lt;body&gt;
  
  &lt;?php
  $temp = "";
  $msg = "";
  $rst = "";
  
  if (isset($_POST["submit"])) {  
  $number = $_POST["custid"];
  
  if(empty($number)) {
  $msg = '&lt;span class="error"&gt; Please enter a value&lt;/span&gt;';
  } else if(!is_numeric($number)) {
  $msg = '&lt;span class="error"&gt; Data entered was not numeric&lt;/span&gt;';
  } else if(strlen($number) != 6) {
  $msg = '&lt;span class="error"&gt; The number entered was not 6 digits long&lt;/span&gt;';
  } else {
  echo "valid";
  }
  
  }
  
  ?&gt;
  
  &lt;h1&gt;Customer Information Collection &lt;br /&gt;&lt;/h1&gt;
  
  &lt;form method="POST" action="&lt;?php echo $_SERVER["PHP_SELF"];?&gt;" id="custinfo" &gt;
  &lt;table&gt;
  &lt;tr&gt;
      &lt;td&gt;&lt;label for="custid"&gt;Customer ID (integer value): &lt;/label&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="text" id="custid" name="custid" value="&lt;?php echo   $temp ?&gt;" size=11 /&gt;&lt;?php echo $msg; ?&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
      &lt;td&gt;&lt;label for="customerfname"&gt;Customer Frist Name: &lt;/label&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="text" id="customerfname" name="fname" size=50/&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
      &lt;td&gt;&lt;label for="customerlname"&gt;Customer Last Name: &lt;/label&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="text" id="customerlname" name="lname" size=50/&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
      &lt;td&gt;&lt;label for="customeraddress"&gt;Customer Address: &lt;/label&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="text" id="customeraddress" name="custaddress" size=65/&gt;&lt;/td&gt;
  
      &lt;td&gt;&lt;label for="suburb"&gt; Suburb: &lt;/label&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input type="text" id="suburb" name="suburb"/&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;
  State:&lt;select name="state" id="state"&gt;
      &lt;option value="select"&gt;--&lt;/option&gt;
      &lt;option value="ACT"&gt;ACT&lt;/option&gt;
      &lt;option value="NSW"&gt;NSW&lt;/option&gt;
      &lt;option value="NT"&gt;NT&lt;/option&gt;
      &lt;option value="QLD"&gt;QLD&lt;/option&gt;
      &lt;option value="SA"&gt;SA&lt;/option&gt;
      &lt;option value="TAS"&gt;TAS&lt;/option&gt;
      &lt;option value="VIC"&gt;VIC&lt;/option&gt;
       &lt;option value="WA"&gt;WA&lt;/option&gt;
    &lt;/select&gt;
  &lt;/td&gt;
  &lt;td&gt;&lt;label for="postcode"&gt; Post Code: &lt;/label&gt;&lt;input type="text" id="postcode" name="postcode" size=4/&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;p&gt;&lt;input type="submit" name="submit" value="Save Data"/&gt;&amp;nbsp;&lt;input type="reset" value="Clear Form" /&gt;
  &lt;/tr&gt;
  
  &lt;/form&gt;
  
  &lt;/body&gt; 
  </code></pre>
  
  <p>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.</p>
  
  <p>Any help would be really great! If any more information is needed, just ask.</p>