SlideShare a Scribd company logo
Lecture 17
You will learn in this Lecture

FormValidation
     - Text Box validation
     - Check Box validation
     - Password validation
     - select list box validation

Before starting with this lecture, There are few questions that deserve to be
answered, What is validation and Why is it requried? Let me answer the first
question and then we will move on to the second question.

So, the first question is, What is validation?
Validation test for required strings contain a specific value, a range of accepted
value or a pattern of acceptable format. For example, If you make a form, and
ask the user to enter name, email address, and comment. If he leaves every field
blank, and click Enter. You will not get any information.
This means we are validating (checking) the values entered by the user, if he has
entered some value or not, if not, then ask him to enter the value and if he has
entered wrong value,then ask him to enter the correct value.

I hope you must have got the answer, why validation is required.

Next question is How validation is done, just read the following lines.

So, as soon as the user clicks Enter, There are two possibilites

1. Send the data that use has entered to the server, and check the values, if
there are any, if there are no values entered by him or if wrong values are
entered by him, ask him to enter the value, and move back to the same form.

2. Or, before sending the data to the server, check whether user has entered any
value or not. If he has entered the value, then the value is corrent or not.

Second method is what we will follow, and is called as client side scripting,
whereas the first method is called as Serve side scripting. JavaScript is popular
for client side scripting and ASP, JSP, PHP are popular for server side scripting.
It is high time to understand the difference between Client Side Scripting and
Server Side Scripting.

When we use a scripting language, that works on the server side, it is called as
Server Side Scripting language,
If we use a scripting language, that works on client side (i.e. browser), it is called
as Client Side Scripting language.
So, Now let us start with validating user input.

When we make a form, we ask the user to enter username, password, and may
ask him to fill up some check boxes, radio butons, and also ask him to write
some comments. It is compulsay for the user to enter username, and password,
but it is not compulsay for him to give his comments. So the things that are
compulsary, cannot be left blank. This is one of the validation, forcing the user
not to leave a field blank.

Let me list down some of the common validations needed
- Password Validation
- Text Field not blank (Name)

Below is the code for Text Box validation
<html>
<head>
      <script language="JavaScript">

       function validate()
       {
       firstName=document.myForm.fname.value;
       lastName=document.myForm.fname.value;
       if(firstName=="")
                window.alert("Name Field cannot be left blank");
       if(lastName=="")
                window.alert("Name Field cannot be left blank");
       switch (firstName.charAt(0))
       {
                case "0":
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "8":
                case "9": window.alert("First chaaracter cannot be a number");
       }
       }

      </script>
</head>
      <body>
      <form name="myForm">
<input type="text" name="fname"> <br>
            <input type="text" name="lname"> <br>
            <input type="submit" onClick="validate()"> <br>
      </form>
      </body>
</html>

And, Now we have the code for Password Validation
<html>
<head>
      <script language="JavaScript">

      function validate()
      {
      passwd=document.myForm.pass;
      cpasswd=document.myForm.cpass;
      if (passwd=="")
              window.alert("Password field cannot be blank");
      if (cpasswd=="")
              window.alert("Confirm Password field cannot be blank");

      if (passwd!=cpasswd)
              window.alert("Passwords dont match");
      }

      </script>
</head>
      <body>
      <form name="myForm">
             Password<input type="password" name="pass"> <br>
             Confirm password<input type="password" name="cpass"> <br>
             <input type="submit" onClick="validate()"> <br>
      </form>
      </body>
</html>

Validate Selection List

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function validateForm(objForm)
{
       var returnStatus = 1;
if (objForm.Make.selectedIndex == 0) {
      alert("Please select a car make");
      returnStatus = 0;
      }
      else
      alert("You selected" +
objForm.Make.options[objForm.Make.selectedIndex].text + objForm.Make.value);

      if (returnStatus) {
      objForm.submit();
      }
}
// -->
</SCRIPT>
</HEAD>
<BODY>
<FORM ACTION="test.asp" NAME="testform">
<SELECT NAME="Make">
       <OPTION VALUE="0" SELECTED>Select One</OPTION>
       <OPTION VALUE="1">Ford</OPTION>
       <OPTION VALUE="2">Chevy</OPTION>
       <OPTION VALUE="3">Pontiac</OPTION>
       <OPTION VALUE="4">Dodge</OPTION>
</SELECT>
<INPUT TYPE="BUTTON" VALUE="Send form"
onClick="validateForm(document.testform)">
</FORM>
</BODY>
</HTML>

Dynamically Populating a Selectin List

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function populate(objForm)
{


      if (objForm.Make.selectedIndex == 0)
      {
      alert("Please select a car make");
      }
      else
      {
alert("You selected" +
objForm.Make.options[objForm.Make.selectedIndex].text + objForm.Make.value);
      if (objForm.Make.selectedIndex == 1)
      {
              objForm.Type.length=2;
              objForm.Type.options[0].text="d1";
              objForm.Type.options[0].value="1";

            objForm.Type.options[1].text="d2";
            objForm.Type.options[1].value="2";
      }

      if (objForm.Make.selectedIndex == 2)
      {
              objForm.Type.length=2;
              objForm.Type.options[0].text="h1";
              objForm.Type.options[0].value="1";

            objForm.Type.options[1].text="h2";
            objForm.Type.options[1].value="2";
      }

      if (objForm.Make.selectedIndex == 3)
      {
              objForm.Type.length=3;
              objForm.Type.options[0].text="m1";
              objForm.Type.options[0].value="1";

            objForm.Type.options[1].text="m2";
            objForm.Type.options[1].value="2";

            objForm.Type.options[2].text="m3";
            objForm.Type.options[2].value="3";
      }

      if (objForm.Make.selectedIndex == 4)
      {
              objForm.Type.length=4;
              objForm.Type.options[0].text="v1";
              objForm.Type.options[0].value="1";

            objForm.Type.options[1].text="v2";
            objForm.Type.options[1].value="2";

            objForm.Type.options[2].text="v3";
            objForm.Type.options[2].value="3";
objForm.Type.options[3].text="v4";
            objForm.Type.options[3].value="4";
      }



      }



}
// -->
</SCRIPT>
</HEAD>
<BODY>
<FORM ACTION="test.asp" NAME="testform">
<SELECT NAME="Make" onChange="populate(document.testform)">
       <OPTION VALUE="0" SELECTED>Select One</OPTION>
       <OPTION VALUE="1">Daewoo</OPTION>
       <OPTION VALUE="2">Hyundae</OPTION>
       <OPTION VALUE="3">Mercedes</OPTION>
       <OPTION VALUE="4">Volswagen</OPTION>
</SELECT>

<SELECT NAME="Type">
</SELECT>
<INPUT TYPE="BUTTON" VALUE="Send form"
onClick="validateForm(document.testform)">
</FORM>
</BODY>
</HTML>

Validating a Check Box

More Related Content

PPT
Form validation client side
PPT
Javascript sivasoft
PPTX
Form Validation in JavaScript
PPTX
Javascript
PDF
Javascript
PPT
Java script Learn Easy
PDF
Javascript essentials
RTF
Java script frame window
 
Form validation client side
Javascript sivasoft
Form Validation in JavaScript
Javascript
Javascript
Java script Learn Easy
Javascript essentials
Java script frame window
 

What's hot (20)

PPT
Form validation server side
PPT
28,29. procedures subprocedure,type checking functions in VBScript
PPTX
Java scriptfunction
PPT
30,31,32,33. decision and loop statements in vbscript
PPT
Java script -23jan2015
PPTX
PPT
Html JavaScript and CSS
PPTX
Java script
PPTX
Java script basics
PPTX
Produce Cleaner Code with Aspect-Oriented Programming
PDF
Intro to JavaScript
PPT
Java script
PPT
PHP - Introduction to PHP Forms
PPT
05 html-forms
ODP
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
PPTX
Java script basic
PDF
Writing clean code
PDF
MVC Design Pattern in JavaScript by ADMEC Multimedia Institute
PDF
Javascript
PDF
Javascript
Form validation server side
28,29. procedures subprocedure,type checking functions in VBScript
Java scriptfunction
30,31,32,33. decision and loop statements in vbscript
Java script -23jan2015
Html JavaScript and CSS
Java script
Java script basics
Produce Cleaner Code with Aspect-Oriented Programming
Intro to JavaScript
Java script
PHP - Introduction to PHP Forms
05 html-forms
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
Java script basic
Writing clean code
MVC Design Pattern in JavaScript by ADMEC Multimedia Institute
Javascript
Javascript
Ad

Viewers also liked (17)

DOC
Html basics 6 image
 
DOC
Java script frame history
 
PDF
Java script objects 2
 
PPT
Week5
 
DOC
Lecture4 isoosi
 
DOC
Lecture2 networkclassification
 
PDF
Assignment4
 
PDF
Assignment sw
 
PDF
Solution2
 
PDF
Set
 
DOC
Lecture1 introductiontonetwork
 
DOC
Html basics 3 font align
 
PDF
Induction
 
DOC
Html basics 10 form
 
DOC
Html basics 1
 
DOC
Html basics 7 table
 
PPT
Week7
 
Html basics 6 image
 
Java script frame history
 
Java script objects 2
 
Week5
 
Lecture4 isoosi
 
Lecture2 networkclassification
 
Assignment4
 
Assignment sw
 
Solution2
 
Set
 
Lecture1 introductiontonetwork
 
Html basics 3 font align
 
Induction
 
Html basics 10 form
 
Html basics 1
 
Html basics 7 table
 
Week7
 
Ad

Similar to Html basics 11 form validation (20)

PPTX
Javascript validating form
PPTX
Form using html and java script validation
PPTX
Web topic 22 validation on web forms
PDF
JavaScript - Chapter 14 - Form Handling
DOCX
Option #1- Form Validation in JavaScript 1- In your text editor- open.docx
PPTX
Java script form validation
PPTX
Javascript ch7
PDF
phptut2
PDF
phptut2
PDF
phptut2
PDF
phptut2
PPTX
Form Validation
PPT
ملخص تقنية تصميم صفحات الويب - الوحدة الرابعة
KEY
Building & Breaking Web Forms with Quaid-JS
PPTX
Project1 VB
PPT
Web Security Mistakes: Trusting The Client
PDF
PDF
Form Validation NG
DOCX
YASH HTML CODES
Javascript validating form
Form using html and java script validation
Web topic 22 validation on web forms
JavaScript - Chapter 14 - Form Handling
Option #1- Form Validation in JavaScript 1- In your text editor- open.docx
Java script form validation
Javascript ch7
phptut2
phptut2
phptut2
phptut2
Form Validation
ملخص تقنية تصميم صفحات الويب - الوحدة الرابعة
Building & Breaking Web Forms with Quaid-JS
Project1 VB
Web Security Mistakes: Trusting The Client
Form Validation NG
YASH HTML CODES

More from H K (20)

PDF
Assignment4
 
DOCX
Assignment3
 
PDF
Solution3
 
DOCX
Mid-
 
PDF
Assignment4
 
PDF
Dm assignment3
 
PPT
Proof
 
PDF
Resolution
 
DOCX
Assignment description
 
PDF
Dm assignment2
 
PDF
Dm assignment1
 
PPTX
Logic
 
DOCX
Introduction
 
PDF
Assignment 2 sol
 
PDF
Assignment sw solution
 
PDF
Violinphoenix
 
PDF
Ie project
 
PDF
Assignment cn subnetting
 
PDF
Assignment uplaodfile
 
PDF
Assignment sw
 
Assignment4
 
Assignment3
 
Solution3
 
Mid-
 
Assignment4
 
Dm assignment3
 
Proof
 
Resolution
 
Assignment description
 
Dm assignment2
 
Dm assignment1
 
Logic
 
Introduction
 
Assignment 2 sol
 
Assignment sw solution
 
Violinphoenix
 
Ie project
 
Assignment cn subnetting
 
Assignment uplaodfile
 
Assignment sw
 

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Business Ethics Teaching Materials for college
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
Cell Structure & Organelles in detailed.
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Insiders guide to clinical Medicine.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Pharma ospi slides which help in ospi learning
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Anesthesia in Laparoscopic Surgery in India
Cardiovascular Pharmacology for pharmacy students.pptx
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
NOI Hackathon - Summer Edition - GreenThumber.pptx
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
How to Manage Starshipit in Odoo 18 - Odoo Slides
O5-L3 Freight Transport Ops (International) V1.pdf
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Business Ethics Teaching Materials for college
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
The Final Stretch: How to Release a Game and Not Die in the Process.
Cell Structure & Organelles in detailed.

Html basics 11 form validation

  • 1. Lecture 17 You will learn in this Lecture FormValidation - Text Box validation - Check Box validation - Password validation - select list box validation Before starting with this lecture, There are few questions that deserve to be answered, What is validation and Why is it requried? Let me answer the first question and then we will move on to the second question. So, the first question is, What is validation? Validation test for required strings contain a specific value, a range of accepted value or a pattern of acceptable format. For example, If you make a form, and ask the user to enter name, email address, and comment. If he leaves every field blank, and click Enter. You will not get any information. This means we are validating (checking) the values entered by the user, if he has entered some value or not, if not, then ask him to enter the value and if he has entered wrong value,then ask him to enter the correct value. I hope you must have got the answer, why validation is required. Next question is How validation is done, just read the following lines. So, as soon as the user clicks Enter, There are two possibilites 1. Send the data that use has entered to the server, and check the values, if there are any, if there are no values entered by him or if wrong values are entered by him, ask him to enter the value, and move back to the same form. 2. Or, before sending the data to the server, check whether user has entered any value or not. If he has entered the value, then the value is corrent or not. Second method is what we will follow, and is called as client side scripting, whereas the first method is called as Serve side scripting. JavaScript is popular for client side scripting and ASP, JSP, PHP are popular for server side scripting. It is high time to understand the difference between Client Side Scripting and Server Side Scripting. When we use a scripting language, that works on the server side, it is called as Server Side Scripting language, If we use a scripting language, that works on client side (i.e. browser), it is called as Client Side Scripting language.
  • 2. So, Now let us start with validating user input. When we make a form, we ask the user to enter username, password, and may ask him to fill up some check boxes, radio butons, and also ask him to write some comments. It is compulsay for the user to enter username, and password, but it is not compulsay for him to give his comments. So the things that are compulsary, cannot be left blank. This is one of the validation, forcing the user not to leave a field blank. Let me list down some of the common validations needed - Password Validation - Text Field not blank (Name) Below is the code for Text Box validation <html> <head> <script language="JavaScript"> function validate() { firstName=document.myForm.fname.value; lastName=document.myForm.fname.value; if(firstName=="") window.alert("Name Field cannot be left blank"); if(lastName=="") window.alert("Name Field cannot be left blank"); switch (firstName.charAt(0)) { case "0": case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9": window.alert("First chaaracter cannot be a number"); } } </script> </head> <body> <form name="myForm">
  • 3. <input type="text" name="fname"> <br> <input type="text" name="lname"> <br> <input type="submit" onClick="validate()"> <br> </form> </body> </html> And, Now we have the code for Password Validation <html> <head> <script language="JavaScript"> function validate() { passwd=document.myForm.pass; cpasswd=document.myForm.cpass; if (passwd=="") window.alert("Password field cannot be blank"); if (cpasswd=="") window.alert("Confirm Password field cannot be blank"); if (passwd!=cpasswd) window.alert("Passwords dont match"); } </script> </head> <body> <form name="myForm"> Password<input type="password" name="pass"> <br> Confirm password<input type="password" name="cpass"> <br> <input type="submit" onClick="validate()"> <br> </form> </body> </html> Validate Selection List <HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript"> <!-- function validateForm(objForm) { var returnStatus = 1;
  • 4. if (objForm.Make.selectedIndex == 0) { alert("Please select a car make"); returnStatus = 0; } else alert("You selected" + objForm.Make.options[objForm.Make.selectedIndex].text + objForm.Make.value); if (returnStatus) { objForm.submit(); } } // --> </SCRIPT> </HEAD> <BODY> <FORM ACTION="test.asp" NAME="testform"> <SELECT NAME="Make"> <OPTION VALUE="0" SELECTED>Select One</OPTION> <OPTION VALUE="1">Ford</OPTION> <OPTION VALUE="2">Chevy</OPTION> <OPTION VALUE="3">Pontiac</OPTION> <OPTION VALUE="4">Dodge</OPTION> </SELECT> <INPUT TYPE="BUTTON" VALUE="Send form" onClick="validateForm(document.testform)"> </FORM> </BODY> </HTML> Dynamically Populating a Selectin List <HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript"> <!-- function populate(objForm) { if (objForm.Make.selectedIndex == 0) { alert("Please select a car make"); } else {
  • 5. alert("You selected" + objForm.Make.options[objForm.Make.selectedIndex].text + objForm.Make.value); if (objForm.Make.selectedIndex == 1) { objForm.Type.length=2; objForm.Type.options[0].text="d1"; objForm.Type.options[0].value="1"; objForm.Type.options[1].text="d2"; objForm.Type.options[1].value="2"; } if (objForm.Make.selectedIndex == 2) { objForm.Type.length=2; objForm.Type.options[0].text="h1"; objForm.Type.options[0].value="1"; objForm.Type.options[1].text="h2"; objForm.Type.options[1].value="2"; } if (objForm.Make.selectedIndex == 3) { objForm.Type.length=3; objForm.Type.options[0].text="m1"; objForm.Type.options[0].value="1"; objForm.Type.options[1].text="m2"; objForm.Type.options[1].value="2"; objForm.Type.options[2].text="m3"; objForm.Type.options[2].value="3"; } if (objForm.Make.selectedIndex == 4) { objForm.Type.length=4; objForm.Type.options[0].text="v1"; objForm.Type.options[0].value="1"; objForm.Type.options[1].text="v2"; objForm.Type.options[1].value="2"; objForm.Type.options[2].text="v3"; objForm.Type.options[2].value="3";
  • 6. objForm.Type.options[3].text="v4"; objForm.Type.options[3].value="4"; } } } // --> </SCRIPT> </HEAD> <BODY> <FORM ACTION="test.asp" NAME="testform"> <SELECT NAME="Make" onChange="populate(document.testform)"> <OPTION VALUE="0" SELECTED>Select One</OPTION> <OPTION VALUE="1">Daewoo</OPTION> <OPTION VALUE="2">Hyundae</OPTION> <OPTION VALUE="3">Mercedes</OPTION> <OPTION VALUE="4">Volswagen</OPTION> </SELECT> <SELECT NAME="Type"> </SELECT> <INPUT TYPE="BUTTON" VALUE="Send form" onClick="validateForm(document.testform)"> </FORM> </BODY> </HTML> Validating a Check Box