SlideShare une entreprise Scribd logo
1  sur  6
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

Contenu connexe

Tendances

Form validation server side
Form validation server side Form validation server side
Form validation server side Mudasir Syed
 
28,29. procedures subprocedure,type checking functions in VBScript
28,29. procedures  subprocedure,type checking functions in VBScript28,29. procedures  subprocedure,type checking functions in VBScript
28,29. procedures subprocedure,type checking functions in VBScriptVARSHAKUMARI49
 
30,31,32,33. decision and loop statements in vbscript
30,31,32,33. decision and loop statements in vbscript30,31,32,33. decision and loop statements in vbscript
30,31,32,33. decision and loop statements in vbscriptVARSHAKUMARI49
 
Produce Cleaner Code with Aspect-Oriented Programming
Produce Cleaner Code with Aspect-Oriented ProgrammingProduce Cleaner Code with Aspect-Oriented Programming
Produce Cleaner Code with Aspect-Oriented ProgrammingPostSharp Technologies
 
05 html-forms
05 html-forms05 html-forms
05 html-formsPalakshya
 
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...Nagios
 
MVC Design Pattern in JavaScript by ADMEC Multimedia Institute
MVC Design Pattern in JavaScript by ADMEC Multimedia InstituteMVC Design Pattern in JavaScript by ADMEC Multimedia Institute
MVC Design Pattern in JavaScript by ADMEC Multimedia InstituteRavi Bhadauria
 

Tendances (20)

Form validation server side
Form validation server side Form validation server side
Form validation server side
 
28,29. procedures subprocedure,type checking functions in VBScript
28,29. procedures  subprocedure,type checking functions in VBScript28,29. procedures  subprocedure,type checking functions in VBScript
28,29. procedures subprocedure,type checking functions in VBScript
 
Java scriptfunction
Java scriptfunctionJava scriptfunction
Java scriptfunction
 
30,31,32,33. decision and loop statements in vbscript
30,31,32,33. decision and loop statements in vbscript30,31,32,33. decision and loop statements in vbscript
30,31,32,33. decision and loop statements in vbscript
 
Java script -23jan2015
Java script -23jan2015Java script -23jan2015
Java script -23jan2015
 
Java Script
Java ScriptJava Script
Java Script
 
Html JavaScript and CSS
Html JavaScript and CSSHtml JavaScript and CSS
Html JavaScript and CSS
 
Java script
Java scriptJava script
Java script
 
Java script basics
Java script basicsJava script basics
Java script basics
 
Produce Cleaner Code with Aspect-Oriented Programming
Produce Cleaner Code with Aspect-Oriented ProgrammingProduce Cleaner Code with Aspect-Oriented Programming
Produce Cleaner Code with Aspect-Oriented Programming
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScript
 
Java script
Java scriptJava script
Java script
 
PHP - Introduction to PHP Forms
PHP - Introduction to PHP FormsPHP - Introduction to PHP Forms
PHP - Introduction to PHP Forms
 
05 html-forms
05 html-forms05 html-forms
05 html-forms
 
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
 
Java script basic
Java script basicJava script basic
Java script basic
 
Writing clean code
Writing clean codeWriting clean code
Writing clean code
 
MVC Design Pattern in JavaScript by ADMEC Multimedia Institute
MVC Design Pattern in JavaScript by ADMEC Multimedia InstituteMVC Design Pattern in JavaScript by ADMEC Multimedia Institute
MVC Design Pattern in JavaScript by ADMEC Multimedia Institute
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 

En vedette

Html basics 6 image
Html basics 6 imageHtml basics 6 image
Html basics 6 imageH K
 
Java script frame history
Java script frame historyJava script frame history
Java script frame historyH K
 
Java script objects 2
Java script objects 2Java script objects 2
Java script objects 2H K
 
Week5
Week5Week5
Week5H K
 
Lecture4 isoosi
Lecture4 isoosiLecture4 isoosi
Lecture4 isoosiH K
 
Lecture2 networkclassification
Lecture2 networkclassificationLecture2 networkclassification
Lecture2 networkclassificationH K
 
Assignment4
Assignment4Assignment4
Assignment4H K
 
Assignment sw
Assignment swAssignment sw
Assignment swH K
 
Solution2
Solution2Solution2
Solution2H K
 
Set
SetSet
SetH K
 
Lecture1 introductiontonetwork
Lecture1 introductiontonetworkLecture1 introductiontonetwork
Lecture1 introductiontonetworkH K
 
Html basics 3 font align
Html basics 3 font alignHtml basics 3 font align
Html basics 3 font alignH K
 
Induction
InductionInduction
InductionH K
 
Html basics 10 form
Html basics 10 formHtml basics 10 form
Html basics 10 formH K
 
Html basics 1
Html basics 1Html basics 1
Html basics 1H K
 
Html basics 7 table
Html basics 7 tableHtml basics 7 table
Html basics 7 tableH K
 
Week7
Week7Week7
Week7H K
 

En vedette (17)

Html basics 6 image
Html basics 6 imageHtml basics 6 image
Html basics 6 image
 
Java script frame history
Java script frame historyJava script frame history
Java script frame history
 
Java script objects 2
Java script objects 2Java script objects 2
Java script objects 2
 
Week5
Week5Week5
Week5
 
Lecture4 isoosi
Lecture4 isoosiLecture4 isoosi
Lecture4 isoosi
 
Lecture2 networkclassification
Lecture2 networkclassificationLecture2 networkclassification
Lecture2 networkclassification
 
Assignment4
Assignment4Assignment4
Assignment4
 
Assignment sw
Assignment swAssignment sw
Assignment sw
 
Solution2
Solution2Solution2
Solution2
 
Set
SetSet
Set
 
Lecture1 introductiontonetwork
Lecture1 introductiontonetworkLecture1 introductiontonetwork
Lecture1 introductiontonetwork
 
Html basics 3 font align
Html basics 3 font alignHtml basics 3 font align
Html basics 3 font align
 
Induction
InductionInduction
Induction
 
Html basics 10 form
Html basics 10 formHtml basics 10 form
Html basics 10 form
 
Html basics 1
Html basics 1Html basics 1
Html basics 1
 
Html basics 7 table
Html basics 7 tableHtml basics 7 table
Html basics 7 table
 
Week7
Week7Week7
Week7
 

Similaire à Html basics 11 form validation

Similaire à Html basics 11 form validation (20)

Chat php
Chat phpChat php
Chat php
 
Javascript
JavascriptJavascript
Javascript
 
Introduction to JavaScript Basics.
Introduction to JavaScript Basics.Introduction to JavaScript Basics.
Introduction to JavaScript Basics.
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
 
Javascript1
Javascript1Javascript1
Javascript1
 
PHP Form Validation Technique
PHP Form Validation TechniquePHP Form Validation Technique
PHP Form Validation Technique
 
javascript.pptx
javascript.pptxjavascript.pptx
javascript.pptx
 
2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security
 
Introduction of javascript
Introduction of javascriptIntroduction of javascript
Introduction of javascript
 
Form demoinplaywithmysql
Form demoinplaywithmysqlForm demoinplaywithmysql
Form demoinplaywithmysql
 
phptut2
phptut2phptut2
phptut2
 
phptut2
phptut2phptut2
phptut2
 
phptut2
phptut2phptut2
phptut2
 
phptut2
phptut2phptut2
phptut2
 
Basics of Java Script (JS)
Basics of Java Script (JS)Basics of Java Script (JS)
Basics of Java Script (JS)
 
full stack practical assignment msc cs.pdf
full stack practical assignment msc cs.pdffull stack practical assignment msc cs.pdf
full stack practical assignment msc cs.pdf
 
JavaScript Training
JavaScript TrainingJavaScript Training
JavaScript Training
 
Js mod1
Js mod1Js mod1
Js mod1
 
Java script
Java scriptJava script
Java script
 
JavaScript - Chapter 14 - Form Handling
 JavaScript - Chapter 14 - Form Handling   JavaScript - Chapter 14 - Form Handling
JavaScript - Chapter 14 - Form Handling
 

Plus de H K

Assignment4
Assignment4Assignment4
Assignment4H K
 
Assignment3
Assignment3Assignment3
Assignment3H K
 
Solution3
Solution3Solution3
Solution3H K
 
Mid-
Mid-Mid-
Mid-H K
 
Assignment4
Assignment4Assignment4
Assignment4H K
 
Dm assignment3
Dm assignment3Dm assignment3
Dm assignment3H K
 
Proof
ProofProof
ProofH K
 
Resolution
ResolutionResolution
ResolutionH K
 
Assignment description
Assignment descriptionAssignment description
Assignment descriptionH K
 
Dm assignment2
Dm assignment2Dm assignment2
Dm assignment2H K
 
Dm assignment1
Dm assignment1Dm assignment1
Dm assignment1H K
 
Logic
LogicLogic
LogicH K
 
Introduction
IntroductionIntroduction
IntroductionH K
 
Assignment 2 sol
Assignment 2 solAssignment 2 sol
Assignment 2 solH K
 
Assignment sw solution
Assignment sw solutionAssignment sw solution
Assignment sw solutionH K
 
Violinphoenix
ViolinphoenixViolinphoenix
ViolinphoenixH K
 
Ie project
Ie projectIe project
Ie projectH K
 
Assignment cn subnetting
Assignment cn subnettingAssignment cn subnetting
Assignment cn subnettingH K
 
Assignment uplaodfile
Assignment uplaodfileAssignment uplaodfile
Assignment uplaodfileH K
 
Assignment sw
Assignment swAssignment sw
Assignment swH K
 

Plus de H K (20)

Assignment4
Assignment4Assignment4
Assignment4
 
Assignment3
Assignment3Assignment3
Assignment3
 
Solution3
Solution3Solution3
Solution3
 
Mid-
Mid-Mid-
Mid-
 
Assignment4
Assignment4Assignment4
Assignment4
 
Dm assignment3
Dm assignment3Dm assignment3
Dm assignment3
 
Proof
ProofProof
Proof
 
Resolution
ResolutionResolution
Resolution
 
Assignment description
Assignment descriptionAssignment description
Assignment description
 
Dm assignment2
Dm assignment2Dm assignment2
Dm assignment2
 
Dm assignment1
Dm assignment1Dm assignment1
Dm assignment1
 
Logic
LogicLogic
Logic
 
Introduction
IntroductionIntroduction
Introduction
 
Assignment 2 sol
Assignment 2 solAssignment 2 sol
Assignment 2 sol
 
Assignment sw solution
Assignment sw solutionAssignment sw solution
Assignment sw solution
 
Violinphoenix
ViolinphoenixViolinphoenix
Violinphoenix
 
Ie project
Ie projectIe project
Ie project
 
Assignment cn subnetting
Assignment cn subnettingAssignment cn subnetting
Assignment cn subnetting
 
Assignment uplaodfile
Assignment uplaodfileAssignment uplaodfile
Assignment uplaodfile
 
Assignment sw
Assignment swAssignment sw
Assignment sw
 

Dernier

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 

Dernier (20)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

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