SlideShare a Scribd company logo
1 of 22
Download to read offline
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

Intro to HTML and CSS basics
Intro to HTML and CSS basicsIntro to HTML and CSS basics
Intro to HTML and CSS basicsEliran Eliassy
 
Form Validation in JavaScript
Form Validation in JavaScriptForm Validation in JavaScript
Form Validation in JavaScriptRavi Bhadauria
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript TutorialBui Kiet
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsEPAM Systems
 
Developing an ASP.NET Web Application
Developing an ASP.NET Web ApplicationDeveloping an ASP.NET Web Application
Developing an ASP.NET Web ApplicationRishi Kothari
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript ObjectsReem Alattas
 
Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)Chris Poteet
 
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...Edureka!
 
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScriptpcnmtutorials
 
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 validationMaitree Patel
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object ModelWebStackAcademy
 
HTML & CSS: Chapter 07
HTML & CSS: Chapter 07HTML & CSS: Chapter 07
HTML & CSS: Chapter 07Steve Guinan
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - ObjectsWebStackAcademy
 

What's hot (20)

Intro to HTML and CSS basics
Intro to HTML and CSS basicsIntro to HTML and CSS basics
Intro to HTML and CSS basics
 
Form Validation in JavaScript
Form Validation in JavaScriptForm Validation in JavaScript
Form Validation in JavaScript
 
Js ppt
Js pptJs ppt
Js ppt
 
JavaScript and BOM events
JavaScript and BOM eventsJavaScript and BOM events
JavaScript and BOM events
 
Web forms in ASP.net
Web forms in ASP.netWeb forms in ASP.net
Web forms in ASP.net
 
Advanced Cascading Style Sheets
Advanced Cascading Style SheetsAdvanced Cascading Style Sheets
Advanced Cascading Style Sheets
 
Namespaces in C#
Namespaces in C#Namespaces in C#
Namespaces in C#
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript Tutorial
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript Basics
 
Developing an ASP.NET Web Application
Developing an ASP.NET Web ApplicationDeveloping an ASP.NET Web Application
Developing an ASP.NET Web Application
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript Objects
 
Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)
 
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
 
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
 
Css
CssCss
Css
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
HTML & CSS: Chapter 07
HTML & CSS: Chapter 07HTML & CSS: Chapter 07
HTML & CSS: Chapter 07
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 

Similar to JavaScript - Chapter 14 - Form Handling

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 tagsJeirahTigas
 
Christopher Latham Portfolio
Christopher Latham PortfolioChristopher Latham Portfolio
Christopher Latham Portfoliolathamcl
 
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 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.pdfHulkTheDevil
 
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 codesilentkiller943187
 
Html tables, forms and audio video
Html tables, forms and audio videoHtml tables, forms and audio video
Html tables, forms and audio videoSaad 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 4Mudasir Syed
 
Html5ppt
Html5pptHtml5ppt
Html5pptrecroup
 

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

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
 
HTML Forms
HTML FormsHTML Forms
HTML 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 JourneyWebStackAcademy
 
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 SecondWebStackAcademy
 
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 CourseWebStackAcademy
 
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 OpportunitiesWebStackAcademy
 
Webstack Academy - Internship Kick Off
Webstack Academy - Internship Kick OffWebstack Academy - Internship Kick Off
Webstack Academy - Internship Kick OffWebStackAcademy
 
Building Your Online Portfolio
Building Your Online PortfolioBuilding Your Online Portfolio
Building Your Online PortfolioWebStackAcademy
 
Front-End Developer's Career Roadmap
Front-End Developer's Career RoadmapFront-End Developer's Career Roadmap
Front-End Developer's Career RoadmapWebStackAcademy
 
Angular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and AuthorizationAngular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and AuthorizationWebStackAcademy
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesWebStackAcademy
 
Angular - Chapter 6 - Firebase Integration
Angular - Chapter 6 - Firebase IntegrationAngular - Chapter 6 - Firebase Integration
Angular - Chapter 6 - Firebase IntegrationWebStackAcademy
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - DirectivesWebStackAcademy
 
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 HandlingWebStackAcademy
 
Angular - Chapter 3 - Components
Angular - Chapter 3 - ComponentsAngular - Chapter 3 - Components
Angular - Chapter 3 - ComponentsWebStackAcademy
 
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 - IntroductionWebStackAcademy
 
JavaScript - Chapter 15 - Debugging Techniques
 JavaScript - Chapter 15 - Debugging Techniques JavaScript - Chapter 15 - Debugging Techniques
JavaScript - Chapter 15 - Debugging TechniquesWebStackAcademy
 
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 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events 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 FunctionsWebStackAcademy
 

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 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 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
 
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

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 

Recently uploaded (20)

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 

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: