![]() Topic awaiting preservation: php form validation? (Page 1 of 1) |
|
---|---|
Bipolar (III) Inmate From: Norway |
![]() Hi! code: <?php if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $epost)) { echo "<p>Ugyldig e-post adresse!</p>"; echo "<a href='javascript:history.back(1);'>Tilbake</a>"; } elseif ($navn == "") { echo "<p>Fyll ut navn!</p>"; echo "<a href='javascript:history.back(1);'>Tilbake</a>"; } elseif ($adresse == "") { echo "<p>Fyll ut Adresse!</p>"; echo "<a href='javascript:history.back(1);'>Tilbake</a>"; } elseif ($postnr == "") { echo "<p>Fyll ut postnummer!</p>"; echo "<a href='javascript:history.back(1);'>Tilbake</a>"; } elseif ($sted == "") { echo "<p>Fyll ut sted!</p>"; echo "<a href='javascript:history.back(1);'>Tilbake</a>"; } elseif ($epost == "") { echo "<p>Fyll ut e-post!</p>"; echo "<a href='javascript:history.back(1);'>Tilbake</a>"; } elseif ($tlf == "") { echo "<p>Fyll ut telefon nummer!</p>"; echo "<a href='javascript:history.back(1);'>Tilbake</a>"; } elseif ($tlf == "") { echo "<p>Fyll ut telefon nummer!</p>"; echo "<a href='javascript:history.back(1);'>Tilbake</a>"; } elseif (mail($to, $subject, $message, $headers)) { echo "<p>Takk $navn, skjemaet er sent! Vi tar kontakt så fort som mulig!</p>"; } else { echo "<p>Skjemaet ble ikke sent!</p>"; } ?>
|
Paranoid (IV) Inmate From: France |
![]() The PHP code is executed once the the page processing the form is loaded, that is when the FORM is submitted, so it's normal that the FORM is submit even though the fields do not match your conditions. code: <form onsubmit="return checkForm( this );"> <!-- your fields --> </form> <script type="text/javascript"> function checkForm( theForm ) { errorArray = [] if( !theForm.elements['epost'].value.match( /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/ ) ) errorArray[ errorArray.length ] = "email address invalid" if( theForm.elements['tlf'].value=="" ) errorArray[ errorArray.length ] = "telephone number invalid" if( !errorArray.length ) return true alert( "** Error **\n\n\t"+ errorArray.join( "\n\t" ) ) return false } </script> |
Paranoid (IV) Inmate From: Florida |
![]() Better to have PHP spit out a "you're a dork, fill in all the fields" page, imo. JavaScript for forms...eww. |
Paranoid (IV) Inmate From: France |
![]() |
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
![]() I usually do both I just think it's a better user experience if you check with JS particularly if there are password fields involved. (I'll always forget one thing, get the next page and then forget to put the passwords again) |
Bipolar (III) Inmate From: Norway |
![]() I'll use JavaScript as well then. Thanks! |
Maniac (V) Mad Scientist From: Rochester, New York, USA |
![]() I will agree, do both. Javascript can make the experience for the user a whole lot easier, but you have to use the server to validate as well or else you will end up having problems that are far more malicious. |
Maniac (V) Inmate From: under the bed |
![]() Can anyone explain how/why using javascript makes a better user experience? |
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
![]() It really tends to depend on the form and the amount of validation needed. For a basic form with a couple of inputs and a textarea or two it's no big deal. |
Maniac (V) Mad Scientist From: Rochester, New York, USA |
![]() You can use your onchange to fire your validation, and then you can have the same type of warning highlight showup on your forms as the user is filling them out. |