SlideShare a Scribd company logo
Team Emertxe
www.webstackacademy.com
Form Handling
JavaScript
www.webstackacademy.com ‹#›www.webstackacademy.com
Introduction to Forms
(Basic form validation)
Form Processing
• Forms are one of the key interaction elements, where the user submits information into the
website
• Upon submission of data into the form (in form of various fields) validation of the form
happens at two levels as follows
Input validation in the
browser itself by scripting
languages like JavaScript
User input is sent via network and
validated at the server side using
server side languages like PHP
Form
Validation
Form Validation – What?
• Form validation is the process of making sure that data supplied by
the user using a form, meets the criteria set for collecting data from
the user. Some examples are:
• Has the user left required field empty ?
• Has the user entered valid email address ?
• Has the user entered text in numeric field ?
• Has the user entered number in correct range ?
• Has the user entered valid date ?
Primitive version of form – Text boxes
• We were using some primitive version of form in form of text boxes
• Combining input boxes with button events can be used to implement forms
<script>
function validate() {
var numValue,formMessage;
numValue = document.getElementById("num").value;
if (isNaN(numValue) || (numValue < 5 || numValue > 20)) {
formMessage = “Not valid!";
} else {
formMessage = "OK";
}
document.getElementById("ex").innerHTML = formMessage;
}
</script>
Primitive version of form – Text boxes
<body>
<p>Please input a number between 5 and 20:</p>
<input id="num">
<button type="button" onclick="validate()">Submit</button>
<p id="ex"></p>
</body>
Accessing Data from forms
Attribute Description
formName Name attribute of the form element
formElement Value of the name attribute
Elements of a form can be accessed by document.formName.formElement
<form name ="form1" onsubmit="return validate(this);">
Username: <input type="text" name="Username"><br>
Password: <input type="password" name="Password"><br>
</form>
Accessing Data from forms
<script>
function validateForm(form){
var Username = document.form1.Username.value;
var Password = document.form1.Password.value;
if (Username.length == 0) {
alert("You must enter a username.");
return false;
}
else if(Password.length < 8){
alert("Password must be at least 6 characters long.");
return false;
}
}
</script>
Form validation – Text fields
Property Description
maxLength Maximum number of character that can be entered in the text field
Placeholder Place default text (ex: some recommendation to the user)
size Specify the field width
disabled Specify whether or not the field is disabled
<input type =”text” name=”tx1” maxLength=”30”>
<input type =”text” name=”tx2” size=”10”>
<input type=”text” name=”tx3” placeholder=”Default Text”>
Form validation – Semantic text fields
Property Description
<input type=”email”> Text field with no line breaks that should hold an email address
<input type=”number”> Single-Line text field for number input
<input type=”url”> Text field with no line breaks that should hold an url.
<input type=“password”> Text field is a password, hence don’t display on the screen
<input type=“submit”> Submit button. Trigger the function that is attached with the
event onsubmit
<input type=“reset”> Reset input fields and take input again
Form validation – Text area
Text Area - Syntax
<textarea name=”field name” id =”id” rows =”nrows” cols =”ncolumns”>
Default text for the field
</textarea>
Exercise
• Enhance the customer enquiry form with the following:
o Add text a text box to enter their enquiry text
o Add additional validation points as follows:
 Text field should not be left blank
 Better email ID validation
 Check there are 10 digits entered for the mobile number
Some useful tips:
• field.value.match(regexp) will match the regular expression against the given value
www.webstackacademy.com ‹#›www.webstackacademy.com
Forms – Additional Features
(More easy way to collect user inputs)
Additional form facilities & validation
Radio button provides option to choose an item from multiple:
Validation code:
<input type="radio" id=“male">Male<br>
<input type="radio" id="female">Female<br>
if ((form.male.checked == false) && (form.female.checked == false) )
{
alert ( "Please choose your Gender: Male or Female" );
return false;
}
Additional form facilities & validation
Checkbox validation (ex: Terms & Condition):
Validation code:
<input type =”checkbox” id=”license”>
if ( document.form1.license.checked == false )
{
alert ( "Please check the Terms & Conditions box." );
return false;
}
Additional form facilities
Select from given set of values
Validation code:
<select id="mySelect">
<option value="apple">Apple</option>
<option value="orange">Orange</option>
</select>
if ( document.form1.city.selectedIndex == 0 )
{
alert ( "Please select your City." );
return false;
}
Exercise
• Write a JavaScript program to create a Pizza order form:
 Create radio button to select pizza (Simply Veg / Veg Supreme / Veg Delight)
 Create radio button to select pizza size (Personal Pan / Medium / Large)
 Create radio button to select crust (Thin / Normal / Special)
 Create a drop down list to select payment option (COD / Credit Card / PayTM / Debit Card)
 Create check box to agree terms & conditions
 Facility to enter discount code with limited size of the field (Have some code like PIZDIS090)
 Take customer details (Name, Email ID and Phone number)
 Display complete details entered by the user
 Add appropriate validation checks as necessary
www.webstackacademy.com ‹#›www.webstackacademy.com
Validation APIs
(Functions provided by JavaScript for form validation)
JavaScript – Validation APIs
Additional validation APIs provided by JavaScript
Property Description
checkValidity() Returns a Boolean indicating whether or not the current value of the element
is valid. It contains automatically populated error message in the field named
“validationMessage”
rangeOverflow Returns true if the element's value is larger than the element's max value.
rangeUnderflow Returns true if the element's value is less than the element's min value.
<input id="id1" type="number" min="100" max="300"
<button onclick="myFunction()">OK</button>
Check Validity example
<script>
function rangeFunction() {
var myInput = document.getElementById("id1“);
if (!myInput.checkValidity())
{
msg = myInput.validationMessage;
}
else
{
msg = "Input OK";
}
document.getElementById("demo").innerHTML = msg;
}
</script>
Range Overflow example
<script>
function rangeFunction() {
var msg = "";
if (document.getElementById("id1").validity.rangeOverflow)
{
msg = "Value too large";
}
else
{
msg = "Input OK";
}
document.getElementById("demo").innerHTML = msg;
}
</script>
WebStack Academy
#83, Farah Towers,
1st Floor, MG Road,
Bangalore – 560001
M: +91-809555 7332
E: training@webstackacademy.com
WSA in Social Media:

More Related Content

What's hot

Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypesVarun C M
 
JavaScript: Events Handling
JavaScript: Events HandlingJavaScript: Events Handling
JavaScript: Events Handling
Yuriy Bezgachnyuk
 
An Introduction to the DOM
An Introduction to the DOMAn Introduction to the DOM
An Introduction to the DOM
Mindy McAdams
 
3. Java Script
3. Java Script3. Java Script
3. Java Script
Jalpesh Vasa
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
shreesenthil
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
Bala Narayanan
 
JavaScript Control Statements I
JavaScript Control Statements IJavaScript Control Statements I
JavaScript Control Statements I
Reem Alattas
 
Javascript
JavascriptJavascript
Javascript
Nagarajan
 
Javascript
JavascriptJavascript
Javascript
Manav Prasad
 
Java script
Java scriptJava script
Java script
Shyam Khant
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
Ravinder Kamboj
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
Priya Goyal
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
Vibrant Technologies & Computers
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
Zeeshan Ahmed
 
Dom(document object model)
Dom(document object model)Dom(document object model)
Dom(document object model)
Partnered Health
 
JavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScriptJavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScript
Laurence Svekis ✔
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
ShahDhruv21
 

What's hot (20)

Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
JavaScript: Events Handling
JavaScript: Events HandlingJavaScript: Events Handling
JavaScript: Events Handling
 
Javascript event handler
Javascript event handlerJavascript event handler
Javascript event handler
 
An Introduction to the DOM
An Introduction to the DOMAn Introduction to the DOM
An Introduction to the DOM
 
3. Java Script
3. Java Script3. Java Script
3. Java Script
 
Java script
Java scriptJava script
Java script
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 
JavaScript Control Statements I
JavaScript Control Statements IJavaScript Control Statements I
JavaScript Control Statements I
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
Java script
Java scriptJava script
Java script
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
Dom(document object model)
Dom(document object model)Dom(document object model)
Dom(document object model)
 
Ajax
AjaxAjax
Ajax
 
JavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScriptJavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScript
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 

Similar to JavaScript - Chapter 14 - Form Handling

Form using html and java script validation
Form using html and java script validationForm using html and java script validation
Form using html and java script validation
Maitree Patel
 
Html forms
Html formsHtml forms
Html forms
eShikshak
 
Html,Css and Javascript Forms using different tags
Html,Css and Javascript Forms using different tagsHtml,Css and Javascript Forms using different tags
Html,Css and Javascript Forms using different tags
JeirahTigas
 
Christopher Latham Portfolio
Christopher Latham PortfolioChristopher Latham Portfolio
Christopher Latham Portfolio
lathamcl
 
Web topic 22 validation on web forms
Web topic 22  validation on web formsWeb topic 22  validation on web forms
Web topic 22 validation on web formsCK Yang
 
Html Forms.ppt
Html Forms.pptHtml Forms.ppt
Html Forms.ppt
MAHASWETAMANDAL1
 
Html basics 11 form validation
Html basics 11 form validationHtml basics 11 form validation
Html basics 11 form validationH K
 
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
HulkTheDevil
 
Html forms
Html formsHtml forms
Html forms
nobel mujuji
 
CSS_Forms.pdf
CSS_Forms.pdfCSS_Forms.pdf
CSS_Forms.pdf
gunjansingh599205
 
FORMS IN PHP contains various tags and code
FORMS IN PHP contains various tags and codeFORMS IN PHP contains various tags and code
FORMS IN PHP contains various tags and code
silentkiller943187
 
Html tables, forms and audio video
Html tables, forms and audio videoHtml tables, forms and audio video
Html tables, forms and audio video
Saad Sheikh
 
Web forms and html lecture Number 4
Web forms and html lecture Number 4Web forms and html lecture Number 4
Web forms and html lecture Number 4
Mudasir Syed
 
Html5ppt
Html5pptHtml5ppt
Html5pptrecroup
 
Html forms
Html formsHtml forms
Wheels
WheelsWheels
Wheels
guest9fd0a95
 
Chapter 9: Forms
Chapter 9: FormsChapter 9: Forms
Chapter 9: Forms
Steve Guinan
 

Similar to JavaScript - Chapter 14 - Form Handling (20)

Form using html and java script validation
Form using html and java script validationForm using html and java script validation
Form using html and java script validation
 
Html forms
Html formsHtml forms
Html forms
 
Html forms
Html formsHtml forms
Html forms
 
Html,Css and Javascript Forms using different tags
Html,Css and Javascript Forms using different tagsHtml,Css and Javascript Forms using different tags
Html,Css and Javascript Forms using different tags
 
Christopher Latham Portfolio
Christopher Latham PortfolioChristopher Latham Portfolio
Christopher Latham Portfolio
 
Web topic 22 validation on web forms
Web topic 22  validation on web formsWeb topic 22  validation on web forms
Web topic 22 validation on web forms
 
Chapter09
Chapter09Chapter09
Chapter09
 
Html Forms.ppt
Html Forms.pptHtml Forms.ppt
Html Forms.ppt
 
Html basics 11 form validation
Html basics 11 form validationHtml basics 11 form validation
Html basics 11 form validation
 
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
 
Html forms
Html formsHtml forms
Html forms
 
CSS_Forms.pdf
CSS_Forms.pdfCSS_Forms.pdf
CSS_Forms.pdf
 
FORMS IN PHP contains various tags and code
FORMS IN PHP contains various tags and codeFORMS IN PHP contains various tags and code
FORMS IN PHP contains various tags and code
 
Html tables, forms and audio video
Html tables, forms and audio videoHtml tables, forms and audio video
Html tables, forms and audio video
 
Web forms and html lecture Number 4
Web forms and html lecture Number 4Web forms and html lecture Number 4
Web forms and html lecture Number 4
 
Html5ppt
Html5pptHtml5ppt
Html5ppt
 
Html forms
Html formsHtml forms
Html forms
 
Wheels
WheelsWheels
Wheels
 
Javascript validating form
Javascript validating formJavascript validating form
Javascript validating form
 
Chapter 9: Forms
Chapter 9: FormsChapter 9: Forms
Chapter 9: Forms
 

More from WebStackAcademy

Webstack Academy - Course Demo Webinar and Placement Journey
Webstack Academy - Course Demo Webinar and Placement JourneyWebstack Academy - Course Demo Webinar and Placement Journey
Webstack Academy - Course Demo Webinar and Placement Journey
WebStackAcademy
 
WSA: Scaling Web Service to Handle Millions of Requests per Second
WSA: Scaling Web Service to Handle Millions of Requests per SecondWSA: Scaling Web Service to Handle Millions of Requests per Second
WSA: Scaling Web Service to Handle Millions of Requests per Second
WebStackAcademy
 
WSA: Course Demo Webinar - Full Stack Developer Course
WSA: Course Demo Webinar - Full Stack Developer CourseWSA: Course Demo Webinar - Full Stack Developer Course
WSA: Course Demo Webinar - Full Stack Developer Course
WebStackAcademy
 
Career Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and OpportunitiesCareer Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy
 
Webstack Academy - Internship Kick Off
Webstack Academy - Internship Kick OffWebstack Academy - Internship Kick Off
Webstack Academy - Internship Kick Off
WebStackAcademy
 
Building Your Online Portfolio
Building Your Online PortfolioBuilding Your Online Portfolio
Building Your Online Portfolio
WebStackAcademy
 
Front-End Developer's Career Roadmap
Front-End Developer's Career RoadmapFront-End Developer's Career Roadmap
Front-End Developer's Career Roadmap
WebStackAcademy
 
Angular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and AuthorizationAngular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
Angular - Chapter 6 - Firebase Integration
Angular - Chapter 6 - Firebase IntegrationAngular - Chapter 6 - Firebase Integration
Angular - Chapter 6 - Firebase Integration
WebStackAcademy
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
WebStackAcademy
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
Angular - Chapter 3 - Components
Angular - Chapter 3 - ComponentsAngular - Chapter 3 - Components
Angular - Chapter 3 - Components
WebStackAcademy
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - Introduction
WebStackAcademy
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
JavaScript - Chapter 15 - Debugging Techniques
 JavaScript - Chapter 15 - Debugging Techniques JavaScript - Chapter 15 - Debugging Techniques
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
JavaScript - Chapter 7 - Advanced Functions
 JavaScript - Chapter 7 - Advanced Functions JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced Functions
WebStackAcademy
 

More from WebStackAcademy (20)

Webstack Academy - Course Demo Webinar and Placement Journey
Webstack Academy - Course Demo Webinar and Placement JourneyWebstack Academy - Course Demo Webinar and Placement Journey
Webstack Academy - Course Demo Webinar and Placement Journey
 
WSA: Scaling Web Service to Handle Millions of Requests per Second
WSA: Scaling Web Service to Handle Millions of Requests per SecondWSA: Scaling Web Service to Handle Millions of Requests per Second
WSA: Scaling Web Service to Handle Millions of Requests per Second
 
WSA: Course Demo Webinar - Full Stack Developer Course
WSA: Course Demo Webinar - Full Stack Developer CourseWSA: Course Demo Webinar - Full Stack Developer Course
WSA: Course Demo Webinar - Full Stack Developer Course
 
Career Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and OpportunitiesCareer Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and Opportunities
 
Webstack Academy - Internship Kick Off
Webstack Academy - Internship Kick OffWebstack Academy - Internship Kick Off
Webstack Academy - Internship Kick Off
 
Building Your Online Portfolio
Building Your Online PortfolioBuilding Your Online Portfolio
Building Your Online Portfolio
 
Front-End Developer's Career Roadmap
Front-End Developer's Career RoadmapFront-End Developer's Career Roadmap
Front-End Developer's Career Roadmap
 
Angular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and AuthorizationAngular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and Authorization
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP Services
 
Angular - Chapter 6 - Firebase Integration
Angular - Chapter 6 - Firebase IntegrationAngular - Chapter 6 - Firebase Integration
Angular - Chapter 6 - Firebase Integration
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
 
Angular - Chapter 3 - Components
Angular - Chapter 3 - ComponentsAngular - Chapter 3 - Components
Angular - Chapter 3 - Components
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - Introduction
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
 
JavaScript - Chapter 15 - Debugging Techniques
 JavaScript - Chapter 15 - Debugging Techniques JavaScript - Chapter 15 - Debugging Techniques
JavaScript - Chapter 15 - Debugging Techniques
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 
JavaScript - Chapter 7 - Advanced Functions
 JavaScript - Chapter 7 - Advanced Functions JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced Functions
 

Recently uploaded

DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 

Recently uploaded (20)

DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 

JavaScript - Chapter 14 - Form Handling

  • 3. Form Processing • Forms are one of the key interaction elements, where the user submits information into the website • Upon submission of data into the form (in form of various fields) validation of the form happens at two levels as follows Input validation in the browser itself by scripting languages like JavaScript User input is sent via network and validated at the server side using server side languages like PHP Form Validation
  • 4. Form Validation – What? • Form validation is the process of making sure that data supplied by the user using a form, meets the criteria set for collecting data from the user. Some examples are: • Has the user left required field empty ? • Has the user entered valid email address ? • Has the user entered text in numeric field ? • Has the user entered number in correct range ? • Has the user entered valid date ?
  • 5. Primitive version of form – Text boxes • We were using some primitive version of form in form of text boxes • Combining input boxes with button events can be used to implement forms <script> function validate() { var numValue,formMessage; numValue = document.getElementById("num").value; if (isNaN(numValue) || (numValue < 5 || numValue > 20)) { formMessage = “Not valid!"; } else { formMessage = "OK"; } document.getElementById("ex").innerHTML = formMessage; } </script>
  • 6. Primitive version of form – Text boxes <body> <p>Please input a number between 5 and 20:</p> <input id="num"> <button type="button" onclick="validate()">Submit</button> <p id="ex"></p> </body>
  • 7. Accessing Data from forms Attribute Description formName Name attribute of the form element formElement Value of the name attribute Elements of a form can be accessed by document.formName.formElement <form name ="form1" onsubmit="return validate(this);"> Username: <input type="text" name="Username"><br> Password: <input type="password" name="Password"><br> </form>
  • 8. Accessing Data from forms <script> function validateForm(form){ var Username = document.form1.Username.value; var Password = document.form1.Password.value; if (Username.length == 0) { alert("You must enter a username."); return false; } else if(Password.length < 8){ alert("Password must be at least 6 characters long."); return false; } } </script>
  • 9. Form validation – Text fields Property Description maxLength Maximum number of character that can be entered in the text field Placeholder Place default text (ex: some recommendation to the user) size Specify the field width disabled Specify whether or not the field is disabled <input type =”text” name=”tx1” maxLength=”30”> <input type =”text” name=”tx2” size=”10”> <input type=”text” name=”tx3” placeholder=”Default Text”>
  • 10. Form validation – Semantic text fields Property Description <input type=”email”> Text field with no line breaks that should hold an email address <input type=”number”> Single-Line text field for number input <input type=”url”> Text field with no line breaks that should hold an url. <input type=“password”> Text field is a password, hence don’t display on the screen <input type=“submit”> Submit button. Trigger the function that is attached with the event onsubmit <input type=“reset”> Reset input fields and take input again
  • 11. Form validation – Text area Text Area - Syntax <textarea name=”field name” id =”id” rows =”nrows” cols =”ncolumns”> Default text for the field </textarea>
  • 12. Exercise • Enhance the customer enquiry form with the following: o Add text a text box to enter their enquiry text o Add additional validation points as follows:  Text field should not be left blank  Better email ID validation  Check there are 10 digits entered for the mobile number Some useful tips: • field.value.match(regexp) will match the regular expression against the given value
  • 13. www.webstackacademy.com ‹#›www.webstackacademy.com Forms – Additional Features (More easy way to collect user inputs)
  • 14. Additional form facilities & validation Radio button provides option to choose an item from multiple: Validation code: <input type="radio" id=“male">Male<br> <input type="radio" id="female">Female<br> if ((form.male.checked == false) && (form.female.checked == false) ) { alert ( "Please choose your Gender: Male or Female" ); return false; }
  • 15. Additional form facilities & validation Checkbox validation (ex: Terms & Condition): Validation code: <input type =”checkbox” id=”license”> if ( document.form1.license.checked == false ) { alert ( "Please check the Terms & Conditions box." ); return false; }
  • 16. Additional form facilities Select from given set of values Validation code: <select id="mySelect"> <option value="apple">Apple</option> <option value="orange">Orange</option> </select> if ( document.form1.city.selectedIndex == 0 ) { alert ( "Please select your City." ); return false; }
  • 17. Exercise • Write a JavaScript program to create a Pizza order form:  Create radio button to select pizza (Simply Veg / Veg Supreme / Veg Delight)  Create radio button to select pizza size (Personal Pan / Medium / Large)  Create radio button to select crust (Thin / Normal / Special)  Create a drop down list to select payment option (COD / Credit Card / PayTM / Debit Card)  Create check box to agree terms & conditions  Facility to enter discount code with limited size of the field (Have some code like PIZDIS090)  Take customer details (Name, Email ID and Phone number)  Display complete details entered by the user  Add appropriate validation checks as necessary
  • 19. JavaScript – Validation APIs Additional validation APIs provided by JavaScript Property Description checkValidity() Returns a Boolean indicating whether or not the current value of the element is valid. It contains automatically populated error message in the field named “validationMessage” rangeOverflow Returns true if the element's value is larger than the element's max value. rangeUnderflow Returns true if the element's value is less than the element's min value. <input id="id1" type="number" min="100" max="300" <button onclick="myFunction()">OK</button>
  • 20. Check Validity example <script> function rangeFunction() { var myInput = document.getElementById("id1“); if (!myInput.checkValidity()) { msg = myInput.validationMessage; } else { msg = "Input OK"; } document.getElementById("demo").innerHTML = msg; } </script>
  • 21. Range Overflow example <script> function rangeFunction() { var msg = ""; if (document.getElementById("id1").validity.rangeOverflow) { msg = "Value too large"; } else { msg = "Input OK"; } document.getElementById("demo").innerHTML = msg; } </script>
  • 22. WebStack Academy #83, Farah Towers, 1st Floor, MG Road, Bangalore – 560001 M: +91-809555 7332 E: training@webstackacademy.com WSA in Social Media: