Re: Form Validation Issues

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Having a giant conditional statement such as the one you have posted is a
real problem for many different reasons. Below is a simple states class that
allows you to test for a state using a couple of different checks (such as
making both comparables lower or upper case). One major issue with the code
block you posted (which may or may not be a problem on your end, it could be
the email software), but these two are not comparable:

<code>
$v = "New Hampshire";
$x = "New
Hampshire";
if ($v === $x) {
   echo("<pre>$v is exactly equal to $x</pre>");
} elseif ($v == $x) {
   echo("<pre>$v is loosely equal to $x</pre>");
} else {
   echo("<pre>$v is not exactly equal to $x</pre>");
}
</code>
This will produce: "New Hampshire is not exactly equal to New
Hampshire"

A better way to test a conditional (whether complex and/or lengthy) is to
wrap it in either a function, or a class method, like so:

<code>
<?php
class States {
   var $suggest = null;
   var $states = Array(
       'alabama'=>true,'al'=>true,
       'alaska'=>true,'ak'=>true,
       'arizona'=>true,'az'=>true,
       'arkansas'=>true,'ar'=>true,
       'california'=>true,'ca'=>true,
       'colorado'=>true,'co'=>true,
       'connecticut'=>true,'ct'=>true,
       'delaware'=>true,'de'=>true,
       'florida'=>true,'fl'=>true,
       'georgia'=>true,'ga'=>true,
       'hawaii'=>true,'hi'=>true,
       'idaho'=>true,'id'=>true,
       'illinois'=>true,'il'=>true,
       'indiana'=>true,'in'=>true,
       'iowa'=>true,'ia'=>true,
       'kansas'=>true,'ks'=>true,
       'kentucky'=>true,'ky'=>true,
       'louisiana'=>true,'la'=>true,
       'maine'=>true,'me'=>true,
       'maryland'=>true,'md'=>true,
       'massachusetts'=>true,'ma'=>true,
       'michigan'=>true,'mi'=>true,
       'minnesota'=>true,'mn'=>true,
       'mississippi'=>true,'ms'=>true,
       'missouri'=>true,'mo'=>true,
       'montana'=>true,'mt'=>true,
       'nebraska'=>true,'ne'=>true,
       'nevada'=>true,'nv'=>true,
       'new hampshire'=>true,'nh'=>true,
       'new jersey'=>true,'nj'=>true,
       'new mexico'=>true,'nm'=>true,
       'new york'=>true,'ny'=>true,
       'north carolina'=>true,'nc'=>true,
       'north dakota'=>true,'nd'=>true,
       'ohio'=>true,'oh'=>true,
       'oklahoma'=>true,'ok'=>true,
       'oregon'=>true,'or'=>true,
       'pennsylvania'=>true,'pa'=>true,
       'rhode island'=>true,'ri'=>true,
       'south carolina'=>true,'sc'=>true,
       'south dakota'=>true,'sd'=>true,
       'tennesee'=>true,'tn'=>true,
       'texas'=>true,'tx'=>true,
       'utah'=>true,'ut'=>true,
       'vermont'=>true,'vt'=>true,
       'virginia'=>true,'va'=>true,
       'washington'=>true,'wa'=>true,
       'west virginia'=>true,'wv'=>true,
       'wisconsin'=>true,'wi'=>true,
       'wyoming'=>true,'wy'=>true
   );
   function States() {
   }
   function isValid($str,$suggest) {
       if ($this->states[strtolower($str)] === true) {
           $this->suggest = null;
           return true;
       } elseif ($suggest === true && strlen($str) > 3) {
           $this->doSuggest($str);
           return false;
       } else {
           $this->suggest = null;
           return false;
       }
   }
   function doSuggest($str) {
       foreach ($this->states as $state => $val) {
           similar_text(strtolower($state),strtolower($str),$result);
           if ($result > 85) {
               $this->suggest = $state;
           }
       }
       if (empty($this->suggest)) {
           $this->suggest = null;
       }
   }
   function isSuggested() {
       return $this->suggest;
   }
}
$states = new States();
$state = 'Hawii';
if ($states->isValid($state,true) === true) {
   echo("<p>$state is a state.</p>");
} elseif ($suggest = $states->isSuggested()) {
   echo("<p>May we suggest $suggest?</p>");
} else {
   echo("<p>State not found.</p>");
}
?>
</code>
--
Jared Farrish
Intermediate Web Developer
Denton, Tx

Abraham Maslow: "If the only tool you have is a hammer, you tend to see
every problem as a nail." $$

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux