| Working with Forms | ||||||||
| Using validation with Forms | ||||||||
| In the example, we created a form that accepts user input, but nothing stops the user to enter incorrect value in the form fields. User may enter invalid email id, e.g. email that does not contains @/. etc. In such a scenario the information coming to server is incorrect and incomplete. | ||||||||
| To keep the information correct, validations are used. Validation assures that the we have already filtered the unwanted values, and we can now assume that, the information available on the server side is accurate. Validation is necessary to insure the quality, accuracy and desirability of data. With validation we can force the user to enter valid value in the respective form fields so that user does not attempt to submit incomplete form. | ||||||||
| There are two types of validation: | ||||||||
| 1. Server side validation
this type of validation uses server-side technologies such as ASP, JSP or PHP and validation is performed on the server.
2. Client side this type of validation runs on client browser and user client side technologies such as Javascript. This type of validation entirely runs on client browser hence reduces the processing load on the server and increases server response time. |
||||||||
| Note: This chapter assumes that you've a sound understanding of Javascript. If you to learn Javascript, please refer to our Javascript tutorial at http://education.ebizel.com. | ||||||||
| Improved Feedback form with Client side validation | ||||||||
| Unlike the previous example which was divided in 2 parts, this example is divided in three parts: | ||||||||
| 1. HTML Form
2. Client Side JavaScript 3. PHP script to process the information |
||||||||
| Part 3, the PHP script is same as the previous example. Part 1 the HTML is same with minor changes in the form declaration. Part 2, the Client side JavaScript force the user to enter data in all mandatory form fields. | ||||||||
| The JavaScript | ||||||||
<?php
if (!isset($_POST['submit'])) {
?>
<script language = "Javascript">
function emailcheck(email) {
var at="@"
var dot="."
var lat=email.indexOf(at)
var lstr=email.length
var ldot=email.indexOf(dot)
if (email.indexOf(at)==-1){
alert("Invalid E-mail ID")
return false
}
if (email.indexOf(at)==-1 | |
||||||||
| The HTML: | ||||||||
| Only the line below has been modified to use the JavaScript: | ||||||||
<form action="<?php echo $PHP_SELF;?>" method="POST" |
||||||||
| Rest, everything is same. | ||||||||
| Click Source to view the source code | ||||||||
| Assignment | ||||||||
| 1. Create an HTML form form.php, which contains 4 fields:
Name (text box), Mobile Number(text box) Gender (radio button) Address (text Area) |
||||||||
| On submitting the form, data goes to form.php which process the data and display it in HTML table, like one given below: | ||||||||
|
||||||||
| 2. in the above HTML form (form.php) use JavaScript validation to check no form field is empty and all fields contains valid values | ||||||||