PHP form validation
This PHP form validation script makes sure that no empty fields are submitted, numeric fields contain numbers only, and checks email against the format and all top level domains. The script contains two files form.php and process.php.
See working sample here.
form.php
|
<!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=iso-8859-1" /> <title> Form </title>
<style type="text/css" > .error{border:1px solid red; } .message{color: red; font-weight:bold; } </style>
</head> <body> <h3>Employee Information</h3> <?php //dislplay error message if (isset($error)) { echo "<p class='message'>" .$error. "</p>" ; } ?> <table width= "327" border= "0" cellspacing= "1" cellpadding= "1" > <form name= "info" id= "info" method= "post" action= "process.php" > <tr> <td width= "82" >Name: </td> <td width= "238" > <input type= "text" name= "name" <?php if($code == 1){ //mark the name fields border in red if empty echo "class='error'" ; } if(isset($name)) { //if not empty display entered info echo "value='" .$name. "'" ; } ?> /> </td> </tr> <tr> <td>Number: </td> <td> <input name= "number" type= "text" id= "number" <?php if($code == 2 || $code == 4){ //mark number field border in red if empty or if not numeric echo "class='error'" ; } if(isset($number)) { //if OK display entered info echo "value='" .$number. "'" ; } ?> /> </td> </tr> <tr> <td> Email: </td> <td> <input name= "email" type= "text" id= "email" <?php if($code == 3 || $code == 5){ //mark email field border in red if empty or if not formated right echo "class='error'" ; } if(isset($email)) { //if OK display entered info echo "value='" .$email. "'" ; } ?> /> </td> </tr> <tr> <td> </td> <td> <input type= "submit" name= "Submit" value= "Submit" /> </td> </tr> </form> </table> </body> </html> |
process.php
This file does all the validation work. If any of the fields is empty it sends the error message back to the form.php. It also checks for numeric fields and proper email format. Notice that email is also checked against all top level domains.
|
<?php //check if name field is empty if(trim($_REQUEST[ 'name' ]) == "" ) { $error= "Field can not be empty!" ; $code= "1" ; header( "Location: index.php?error=$error&code=$code&name=$name&email=$email&number=$number" ); exit() ; }
//check if number field is empty if(trim($_REQUEST[ 'number' ]) == "" ) { $error= "Field can not be empty!" ; $code= "2" ; header( "Location: index.php?error=$error&code=$code&name=$name&email=$email&number=$number" ); exit() ; } //check if email field is empty if(trim($_REQUEST[ 'email' ]) == "" ) { $error= "Field can not be empty!" ; $code= "3" ; header( "Location: index.php?error=$error&code=$code&name=$name&email=$email&number=$number" ); exit() ; }
//check if the number field is numeric if(is_numeric(trim($_REQUEST[ 'number' ])) == false ) { $error= "Please enter numeric value" ; $code=4; header( "Location: index.php?error=$error&code=$code&name=$name&email=$email&number=$number" ); exit() ; }
//check for valid email against format and all top level domain names if( !ereg( "^([^[:space:]]+)@(.+).(ad|ae|af|ag|ai|al|am|an|ao|aq| ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs| bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|cr|cu|cv| cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|fi|fj|fk|fm|fo| fr|fx|ga|gb|gov|gd|ge|gf|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy| hk|hm|hn|hr|ht|hu|id|ie|il|in|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh| ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md| mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nato|nc| ne|net|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn| pr|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so| sr|st|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug| uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$" ,$_REQUEST[ 'email' ])) { $error= "Please enter valid email" ; $code= 5 ; header( "Location: index.php?error=$error&code=$code&name=$name&email=$email&number=$number" ); exit() ; }
//Below should be the code that process information submited. In this tutorial we will just display the output in the browser. echo "Name: " .$name. "<br />Number: " .$number. "<br />Email: " .$email ; ?>
|