Form Validation
When you create a form for submission,It is very important to validate forms before submission to avoid filling your data base with incorrect data.
Form Validation In PHPName: | |
create following php you will get error messages with empty fields and if name field contain less than 6 characters.
<?php $errors = array(); $name=''; $email=''; if(isset($_POST['name']) && isset($_POST['email'])) { $name = $_POST['name']; $email = $_POST['email']; if (strlen($name) < 6) { $errors['name'] = 'Minimum 6 Characters Required'; } if(empty($name) || empty($email)) { $errors['required'] = 'All fields required'; } } if(empty($errors) && $_POST) { echo '<font color=#91100d><b>Your form has been submitted</b></font>'; } elseif ($errors || !$_POST) { ?> <form name="form" action="" method="post"> <font color=#91100d><b><?PHP if (isset($errors['required'])) { echo $errors['required'];} ?></b></font> <table bgcolor=#9ca9c9> <tr bgcolor=#e7eaf2><td> Name:</td><td> <input type="text" name="name" value="<?php echo $name ?>"> <font color=#91100d><b><?PHP if (isset($errors['name'])){ echo $errors['name']; } ?></b></font></td></tr> <tr bgcolor=#e7eaf2><td>Email</td><td> <input type="text" name="email"></td></tr> <tr bgcolor=#e7eaf2><td colspan=2 align="right"><input type="submit" name="submit" value="submit" style="width:200px;border: 1px dashed #BBB;background-color: #c6eef1"} ></td></tr> </form> </table> <?php } ?> |
Following I have created form validation using java scripts(Please comment I have good collection of java scripts too)
Name | |
Phone Number | |
Country | |
<html> <head> <title>Form Validation</title> <script type="text/javascript"> <!-- // Form validation code will come here. function validate() { if( document.myForm.Name.value == "" ) { alert( "Please provide your name!" ); document.myForm.Name.focus() ; return false; } if( document.myForm.EMail.value == "" ) { alert( "Please provide your Email!" ); document.myForm.EMail.focus() ; return false; } if( document.myForm.phone.value == "" || isNaN( document.myForm.phone.value ) || document.myForm.phone.value.length != 10 ) { alert( "Please provide a phone in numbers and digits is more than 10" ); document.myForm.phone.focus() ; return false; } if( document.myForm.Country.value == "-1" ) { alert( "Please provide your country!" ); return false; } return( true ); } //--> </script> </head> <body> <form action="" name="myForm" onsubmit="return(validate());"> <table bgcolor=#eac6c5 align="center"> <tr> <td align="right">Name</td> <td><input type="text" name="Name" /></td> </tr> <tr> <td align="right">EMail</td> <td><input type="text" name="EMail" /></td> </tr> <tr> <td align="right">Phone Number</td> <td><input type="text" name="phone" /></td> </tr> <tr> <td align="right">Country</td> <td> <select name="Country"> <option value="-1" selected>[choose country]</option> <option value="1">USA</option> <option value="2">UK</option> <option value="3">CANADA</option> </select> </td> </tr> <tr> <td align="right"></td> <td><input type="submit" value="Submit" /></td> </tr> </table> </form> </body> </html> |
Validating a UK telephone number is a lot more complicated than you might first imagine.
ReplyDeletehttps://github.com/g1smd/Drupal-CCK-Phone-GB/blob/master/phone.gb.inc
The above code allows a wide variety of input formats, checks the number range and length in detail, and then applies strict formatting for each number range and length.