SlideShare ist ein Scribd-Unternehmen logo
1 von 14
Form Validation A Simple Javascript Exercise
Sending eMessages A alternative to email Message is stored in company database But how do we check that the requires fields are filled in? Do this at the client side using javascript
The Form Six fields – the first four are of type="text", the fifth is a drop down selectlistbox, and the last is a textarea. Note that the submit button is disabled.
The XHTML Markup #1 <body>  <imgsrc="bdlogo.gif“  />  <form id="emessage" method="post" action="msgadd.php">  <fieldset style="border:none"> ……….  </fieldset> </body> First, the image, form and fieldset tags. Remember that the XHTML  Strict doctype requires all form elements to be contained inside a fieldset or similar structure . Note also that the name attribute is not used in the form tag, instead we use id.
The XHTML Markup #2 <body> <form id="emessage" method="post" action="msgadd.php"> <fieldset style="border:none">  ……. <label for="name">Name: </label>  <input type="text" name="name" id="name" onclick="enable()" style="width:240" /><span style="color:red"> * </span><br />  <label for="email">Email: </label>  <input type="text" name="email" id="email" style="width:240"/><span style="color:red"> * </span><br />  <label for="phone">Telephone No: </label>  <input type="text" name="phone" id="phone" style="width:140" /><br />  <label for="sales">Sales/Returns<br />Order No: </label>  <input type="text" name="sales" id="sales" style="width:100" /><br /> ………  </fieldset> </form> </body> Next, the labels and input fields of type="text“. Note the onclick event handler called enable() in the name field – more on this later
The XHTML Markup #3 <body> <form id="emessage" method="post" action="msgadd.php"> <fieldset style="border:none">  ……. <label for="enq">Nature of<br />Enquiry: </label> <select name="enq" id="enq" style="width:240" />   <option value="0">Please Select</option>   <option value="Defective Goods">Defective Goods</option>   <option value="Missing Items">Missing Items</option>    <option value="Non-Delivery">Non-Delivery</option>  </select><span style="color:red"> * </span><br /><br />  ………  </fieldset> </form> </body> Next, the label and drop down selectlistbox
The XHTML Markup #4 <body> <form id="emessage" method="post" action="msgadd.php"> <fieldset style="border:none">  …….  <label for="message">Message: </label>  <textarea rows="20" cols="60" name="message" id="message"></textarea><span style="color:red"> * </span><br />     <br />  <input type="submit" name="submit" id="submit" value="Send" disabled="disabled" onclick="return checkmessages()" />   <input type="reset" name="reset" id="reset" value="Reset" /> ………  </fieldset> </form> </body> Last, the textareaand submit and reset buttons. Note that the submit button is initially disabled. Note also it’s onclick event handler – we use this to check that the form fields are filled in as required
The Required Fields There are four required fields on the form marked with a red asterix (*) When the user clicks the submit button we need to check that they have all been filled in We do this using a javascript function called checkmessages() This is invoked when the user clicks the submit button Our javascript code goes in the <head> section of the document and must be enclosed in <script> .. </script> tags When the user clicks in the name field the submit button will be enabled.
The enable() Function function enable() { document.getElementById("emessage").submit.disabled=false; } This is the code that enables the submit button once the user clicks in the name field. Note the use of the document.getElementById() function – this is our way of accessing the form elements.
The checkmessages() Function function checkmessages() { vartheMessage = "Invalid Message - Please complete the following: ------------------------------------------------------------------------"; varnoErrors = true;    if (document.getElementById("emessage").name.value=="") { theMessage = theMessage + " --> Name"; noErrors = false;    }    // If no errors, submit the form    if (noErrors) {       return true;    } else {     // errors were found, show alert message     alert(theMessage); document.getElementById("emessage").submit.disabled=true;     return false;    }  } // end of function checkmessages() This is the code that checks the name field. To check the remaining fields you only need copy and paste this and edit  the name of the field being checked. For the select field however you will need to check if the value is "0"
Styling the Form To pretty up the form we use a style sheet: <style>  label{   float: left;   width: 180px;   font-weight: bold;  } textarea,select,input{  margin-bottom: 5px;  }  #submit{  margin-left: 200px;  margin-top: 5px;  width: 100px; } br{  clear: left; } </style>
Warning the User If a required field is not filled in we output a warning message to this effect on the screen Let’s assume the name field isn’t filled in.
Confirming Form Submission Once the form has been successfully submitted we need to inform the user of this This can be done with a simple php script (see next slide).
Success!

Weitere ähnliche Inhalte

Was ist angesagt?

WP7 HUB_Diseño del interfaz con Silverlight
WP7 HUB_Diseño del interfaz con SilverlightWP7 HUB_Diseño del interfaz con Silverlight
WP7 HUB_Diseño del interfaz con SilverlightMICTT Palma
 
Php Form
Php FormPhp Form
Php Formlotlot
 
Entering User Data from a Web Page HTML Forms
Entering User Data from a Web Page HTML FormsEntering User Data from a Web Page HTML Forms
Entering User Data from a Web Page HTML Formssathish sak
 
Web engineering - HTML Form
Web engineering -  HTML FormWeb engineering -  HTML Form
Web engineering - HTML FormNosheen Qamar
 
HTML 5 Simple Tutorial Part 4
HTML 5 Simple Tutorial Part 4HTML 5 Simple Tutorial Part 4
HTML 5 Simple Tutorial Part 4Sanjeev Kumar
 
HTML::FormFu talk for Sydney PM
HTML::FormFu talk for Sydney PMHTML::FormFu talk for Sydney PM
HTML::FormFu talk for Sydney PMDean Hamstead
 
Building a Blogging System -- Rapidly using Alpha Five v10 with Codeless AJAX...
Building a Blogging System -- Rapidly using Alpha Five v10 with Codeless AJAX...Building a Blogging System -- Rapidly using Alpha Five v10 with Codeless AJAX...
Building a Blogging System -- Rapidly using Alpha Five v10 with Codeless AJAX...Richard Rabins
 
JavaScript Training in Ambala ! Batra Computer Centre
JavaScript Training in Ambala ! Batra Computer CentreJavaScript Training in Ambala ! Batra Computer Centre
JavaScript Training in Ambala ! Batra Computer Centrejatin batra
 
Acceptance Testing With Selenium
Acceptance Testing With SeleniumAcceptance Testing With Selenium
Acceptance Testing With Seleniumelliando dias
 
Php and web forms
Php and web formsPhp and web forms
Php and web formssana mateen
 
e computer notes - Creating views
e computer notes - Creating viewse computer notes - Creating views
e computer notes - Creating viewsecomputernotes
 
Access tips access and sql part 5 more instant queries 1
Access tips  access and sql part 5  more instant queries 1Access tips  access and sql part 5  more instant queries 1
Access tips access and sql part 5 more instant queries 1quest2900
 
Esd input inplace
Esd input inplaceEsd input inplace
Esd input inplaceAnumadil1
 
Mechanize at the Ruby Drink-up of Sophia, November 2011
Mechanize at the Ruby Drink-up of Sophia, November 2011Mechanize at the Ruby Drink-up of Sophia, November 2011
Mechanize at the Ruby Drink-up of Sophia, November 2011rivierarb
 

Was ist angesagt? (18)

WP7 HUB_Diseño del interfaz con Silverlight
WP7 HUB_Diseño del interfaz con SilverlightWP7 HUB_Diseño del interfaz con Silverlight
WP7 HUB_Diseño del interfaz con Silverlight
 
Php Form
Php FormPhp Form
Php Form
 
Entering User Data from a Web Page HTML Forms
Entering User Data from a Web Page HTML FormsEntering User Data from a Web Page HTML Forms
Entering User Data from a Web Page HTML Forms
 
Web engineering - HTML Form
Web engineering -  HTML FormWeb engineering -  HTML Form
Web engineering - HTML Form
 
PHP Making Web Forms
PHP Making Web FormsPHP Making Web Forms
PHP Making Web Forms
 
HTML 5 Simple Tutorial Part 4
HTML 5 Simple Tutorial Part 4HTML 5 Simple Tutorial Part 4
HTML 5 Simple Tutorial Part 4
 
HTML::FormFu talk for Sydney PM
HTML::FormFu talk for Sydney PMHTML::FormFu talk for Sydney PM
HTML::FormFu talk for Sydney PM
 
Building a Blogging System -- Rapidly using Alpha Five v10 with Codeless AJAX...
Building a Blogging System -- Rapidly using Alpha Five v10 with Codeless AJAX...Building a Blogging System -- Rapidly using Alpha Five v10 with Codeless AJAX...
Building a Blogging System -- Rapidly using Alpha Five v10 with Codeless AJAX...
 
JavaScript Training in Ambala ! Batra Computer Centre
JavaScript Training in Ambala ! Batra Computer CentreJavaScript Training in Ambala ! Batra Computer Centre
JavaScript Training in Ambala ! Batra Computer Centre
 
Acceptance Testing With Selenium
Acceptance Testing With SeleniumAcceptance Testing With Selenium
Acceptance Testing With Selenium
 
Exception handling
Exception handlingException handling
Exception handling
 
phptut2
phptut2phptut2
phptut2
 
Php and web forms
Php and web formsPhp and web forms
Php and web forms
 
e computer notes - Creating views
e computer notes - Creating viewse computer notes - Creating views
e computer notes - Creating views
 
Access tips access and sql part 5 more instant queries 1
Access tips  access and sql part 5  more instant queries 1Access tips  access and sql part 5  more instant queries 1
Access tips access and sql part 5 more instant queries 1
 
Esd input inplace
Esd input inplaceEsd input inplace
Esd input inplace
 
Mechanize at the Ruby Drink-up of Sophia, November 2011
Mechanize at the Ruby Drink-up of Sophia, November 2011Mechanize at the Ruby Drink-up of Sophia, November 2011
Mechanize at the Ruby Drink-up of Sophia, November 2011
 
Html4
Html4Html4
Html4
 

Andere mochten auch

Javascript validation karan chanana
Javascript validation karan chananaJavascript validation karan chanana
Javascript validation karan chananakaranchanan
 
Javascript validation assignment
Javascript validation assignmentJavascript validation assignment
Javascript validation assignmentH K
 
02. input validation module v5
02. input validation module v502. input validation module v5
02. input validation module v5Eoin Keary
 
1 - Designing A Site
1 - Designing A Site1 - Designing A Site
1 - Designing A SiteGraeme Smith
 
Accessible dynamic forms
Accessible dynamic formsAccessible dynamic forms
Accessible dynamic formsDylan Barrell
 
Web forms and server side scripting
Web forms and server side scriptingWeb forms and server side scripting
Web forms and server side scriptingsawsan slii
 

Andere mochten auch (11)

Javascript validating form
Javascript validating formJavascript validating form
Javascript validating form
 
Javascript validation karan chanana
Javascript validation karan chananaJavascript validation karan chanana
Javascript validation karan chanana
 
Javascript validation assignment
Javascript validation assignmentJavascript validation assignment
Javascript validation assignment
 
02. input validation module v5
02. input validation module v502. input validation module v5
02. input validation module v5
 
1 - Designing A Site
1 - Designing A Site1 - Designing A Site
1 - Designing A Site
 
Accessible dynamic forms
Accessible dynamic formsAccessible dynamic forms
Accessible dynamic forms
 
Web forms and server side scripting
Web forms and server side scriptingWeb forms and server side scripting
Web forms and server side scripting
 
Ch3 server controls
Ch3 server controlsCh3 server controls
Ch3 server controls
 
Javascript
JavascriptJavascript
Javascript
 
Java script ppt
Java script pptJava script ppt
Java script ppt
 
Javascript Best Practices
Javascript Best PracticesJavascript Best Practices
Javascript Best Practices
 

Ähnlich wie Form Validation Javascript

Lecture 2 - Comm Lab: Web @ ITP
Lecture 2 - Comm Lab: Web @ ITPLecture 2 - Comm Lab: Web @ ITP
Lecture 2 - Comm Lab: Web @ ITPyucefmerhi
 
We9 Struts 2.0
We9 Struts 2.0We9 Struts 2.0
We9 Struts 2.0wangjiaz
 
Quick Referance to WML
Quick Referance to WMLQuick Referance to WML
Quick Referance to WMLNitin Saswade
 
Grddl In A Nutshell V1
Grddl In A Nutshell V1Grddl In A Nutshell V1
Grddl In A Nutshell V1Fabien Gandon
 
KMUTNB - Internet Programming 3/7
KMUTNB - Internet Programming 3/7KMUTNB - Internet Programming 3/7
KMUTNB - Internet Programming 3/7phuphax
 
Java Script
Java ScriptJava Script
Java Scriptsiddaram
 
Neil Patel - What You Need to be Measuring and How to Do It
Neil Patel - What You Need to be Measuring and How to Do ItNeil Patel - What You Need to be Measuring and How to Do It
Neil Patel - What You Need to be Measuring and How to Do ItCarsonified Team
 
Developing and testing ajax components
Developing and testing ajax componentsDeveloping and testing ajax components
Developing and testing ajax componentsIgnacio Coloma
 
Php Calling Operators
Php Calling OperatorsPhp Calling Operators
Php Calling Operatorsmussawir20
 
From "Username and Password" to InfoCard
From "Username and Password" to InfoCardFrom "Username and Password" to InfoCard
From "Username and Password" to InfoCardgoodfriday
 
SES Chicago "Developments in Information Retrieval on the Web"
SES Chicago "Developments in Information Retrieval on the Web"SES Chicago "Developments in Information Retrieval on the Web"
SES Chicago "Developments in Information Retrieval on the Web"Jay Myers
 
Lecture 5 - Comm Lab: Web @ ITP
Lecture 5 - Comm Lab: Web @ ITPLecture 5 - Comm Lab: Web @ ITP
Lecture 5 - Comm Lab: Web @ ITPyucefmerhi
 
Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Coursemussawir20
 

Ähnlich wie Form Validation Javascript (20)

Lecture 2 - Comm Lab: Web @ ITP
Lecture 2 - Comm Lab: Web @ ITPLecture 2 - Comm Lab: Web @ ITP
Lecture 2 - Comm Lab: Web @ ITP
 
We9 Struts 2.0
We9 Struts 2.0We9 Struts 2.0
We9 Struts 2.0
 
Quick Referance to WML
Quick Referance to WMLQuick Referance to WML
Quick Referance to WML
 
Lecture3
Lecture3Lecture3
Lecture3
 
Grddl In A Nutshell V1
Grddl In A Nutshell V1Grddl In A Nutshell V1
Grddl In A Nutshell V1
 
KMUTNB - Internet Programming 3/7
KMUTNB - Internet Programming 3/7KMUTNB - Internet Programming 3/7
KMUTNB - Internet Programming 3/7
 
Ajax ons2
Ajax ons2Ajax ons2
Ajax ons2
 
Zend Form Tutorial
Zend Form TutorialZend Form Tutorial
Zend Form Tutorial
 
JSP Custom Tags
JSP Custom TagsJSP Custom Tags
JSP Custom Tags
 
Java Script
Java ScriptJava Script
Java Script
 
Lecture1 B Frames&Forms
Lecture1 B  Frames&FormsLecture1 B  Frames&Forms
Lecture1 B Frames&Forms
 
Neil Patel - What You Need to be Measuring and How to Do It
Neil Patel - What You Need to be Measuring and How to Do ItNeil Patel - What You Need to be Measuring and How to Do It
Neil Patel - What You Need to be Measuring and How to Do It
 
Developing and testing ajax components
Developing and testing ajax componentsDeveloping and testing ajax components
Developing and testing ajax components
 
Widgets Tools Keynote
Widgets Tools KeynoteWidgets Tools Keynote
Widgets Tools Keynote
 
Php Calling Operators
Php Calling OperatorsPhp Calling Operators
Php Calling Operators
 
From "Username and Password" to InfoCard
From "Username and Password" to InfoCardFrom "Username and Password" to InfoCard
From "Username and Password" to InfoCard
 
SES Chicago "Developments in Information Retrieval on the Web"
SES Chicago "Developments in Information Retrieval on the Web"SES Chicago "Developments in Information Retrieval on the Web"
SES Chicago "Developments in Information Retrieval on the Web"
 
Lecture 5 - Comm Lab: Web @ ITP
Lecture 5 - Comm Lab: Web @ ITPLecture 5 - Comm Lab: Web @ ITP
Lecture 5 - Comm Lab: Web @ ITP
 
Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Course
 
YQL talk at OHD Jakarta
YQL talk at OHD JakartaYQL talk at OHD Jakarta
YQL talk at OHD Jakarta
 

Mehr von Graeme Smith

Project Management - An Introductiuon
Project Management - An IntroductiuonProject Management - An Introductiuon
Project Management - An IntroductiuonGraeme Smith
 
The art of presentations
The art of presentationsThe art of presentations
The art of presentationsGraeme Smith
 
Typography, A Presentation
Typography, A PresentationTypography, A Presentation
Typography, A PresentationGraeme Smith
 
Typography and grids
Typography and gridsTypography and grids
Typography and gridsGraeme Smith
 
Intro to Game Design
Intro to Game DesignIntro to Game Design
Intro to Game DesignGraeme Smith
 
Composition, some basic rules of photography
Composition, some basic rules of photographyComposition, some basic rules of photography
Composition, some basic rules of photographyGraeme Smith
 
Personality Emotion
Personality EmotionPersonality Emotion
Personality EmotionGraeme Smith
 
Personality Emotion
Personality EmotionPersonality Emotion
Personality EmotionGraeme Smith
 
Information Design
Information DesignInformation Design
Information DesignGraeme Smith
 
Perspective Principles
Perspective PrinciplesPerspective Principles
Perspective PrinciplesGraeme Smith
 
Interaction Design
Interaction DesignInteraction Design
Interaction DesignGraeme Smith
 
Layout Principles 1
Layout Principles 1Layout Principles 1
Layout Principles 1Graeme Smith
 
Content for the Web
Content for the WebContent for the Web
Content for the WebGraeme Smith
 

Mehr von Graeme Smith (20)

Project Management - An Introductiuon
Project Management - An IntroductiuonProject Management - An Introductiuon
Project Management - An Introductiuon
 
Intro to the unit
Intro to the unitIntro to the unit
Intro to the unit
 
The CSS Box Model
The CSS Box ModelThe CSS Box Model
The CSS Box Model
 
The art of presentations
The art of presentationsThe art of presentations
The art of presentations
 
Typography, A Presentation
Typography, A PresentationTypography, A Presentation
Typography, A Presentation
 
Flip Book
Flip BookFlip Book
Flip Book
 
Typography
TypographyTypography
Typography
 
Typography and grids
Typography and gridsTypography and grids
Typography and grids
 
Intro to Game Design
Intro to Game DesignIntro to Game Design
Intro to Game Design
 
Composition, some basic rules of photography
Composition, some basic rules of photographyComposition, some basic rules of photography
Composition, some basic rules of photography
 
Personality Emotion
Personality EmotionPersonality Emotion
Personality Emotion
 
Personality Emotion
Personality EmotionPersonality Emotion
Personality Emotion
 
Information Design
Information DesignInformation Design
Information Design
 
Perspective Principles
Perspective PrinciplesPerspective Principles
Perspective Principles
 
Interaction Design
Interaction DesignInteraction Design
Interaction Design
 
Colour
ColourColour
Colour
 
Web Functionality
Web FunctionalityWeb Functionality
Web Functionality
 
Layout Principles
Layout PrinciplesLayout Principles
Layout Principles
 
Layout Principles 1
Layout Principles 1Layout Principles 1
Layout Principles 1
 
Content for the Web
Content for the WebContent for the Web
Content for the Web
 

Kürzlich hochgeladen

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 

Kürzlich hochgeladen (20)

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Form Validation Javascript

  • 1. Form Validation A Simple Javascript Exercise
  • 2. Sending eMessages A alternative to email Message is stored in company database But how do we check that the requires fields are filled in? Do this at the client side using javascript
  • 3. The Form Six fields – the first four are of type="text", the fifth is a drop down selectlistbox, and the last is a textarea. Note that the submit button is disabled.
  • 4. The XHTML Markup #1 <body> <imgsrc="bdlogo.gif“ /> <form id="emessage" method="post" action="msgadd.php"> <fieldset style="border:none"> ………. </fieldset> </body> First, the image, form and fieldset tags. Remember that the XHTML Strict doctype requires all form elements to be contained inside a fieldset or similar structure . Note also that the name attribute is not used in the form tag, instead we use id.
  • 5. The XHTML Markup #2 <body> <form id="emessage" method="post" action="msgadd.php"> <fieldset style="border:none"> ……. <label for="name">Name: </label> <input type="text" name="name" id="name" onclick="enable()" style="width:240" /><span style="color:red"> * </span><br /> <label for="email">Email: </label> <input type="text" name="email" id="email" style="width:240"/><span style="color:red"> * </span><br /> <label for="phone">Telephone No: </label> <input type="text" name="phone" id="phone" style="width:140" /><br /> <label for="sales">Sales/Returns<br />Order No: </label> <input type="text" name="sales" id="sales" style="width:100" /><br /> ……… </fieldset> </form> </body> Next, the labels and input fields of type="text“. Note the onclick event handler called enable() in the name field – more on this later
  • 6. The XHTML Markup #3 <body> <form id="emessage" method="post" action="msgadd.php"> <fieldset style="border:none"> ……. <label for="enq">Nature of<br />Enquiry: </label> <select name="enq" id="enq" style="width:240" /> <option value="0">Please Select</option> <option value="Defective Goods">Defective Goods</option> <option value="Missing Items">Missing Items</option> <option value="Non-Delivery">Non-Delivery</option> </select><span style="color:red"> * </span><br /><br /> ……… </fieldset> </form> </body> Next, the label and drop down selectlistbox
  • 7. The XHTML Markup #4 <body> <form id="emessage" method="post" action="msgadd.php"> <fieldset style="border:none"> ……. <label for="message">Message: </label> <textarea rows="20" cols="60" name="message" id="message"></textarea><span style="color:red"> * </span><br /> <br /> <input type="submit" name="submit" id="submit" value="Send" disabled="disabled" onclick="return checkmessages()" /> <input type="reset" name="reset" id="reset" value="Reset" /> ……… </fieldset> </form> </body> Last, the textareaand submit and reset buttons. Note that the submit button is initially disabled. Note also it’s onclick event handler – we use this to check that the form fields are filled in as required
  • 8. The Required Fields There are four required fields on the form marked with a red asterix (*) When the user clicks the submit button we need to check that they have all been filled in We do this using a javascript function called checkmessages() This is invoked when the user clicks the submit button Our javascript code goes in the <head> section of the document and must be enclosed in <script> .. </script> tags When the user clicks in the name field the submit button will be enabled.
  • 9. The enable() Function function enable() { document.getElementById("emessage").submit.disabled=false; } This is the code that enables the submit button once the user clicks in the name field. Note the use of the document.getElementById() function – this is our way of accessing the form elements.
  • 10. The checkmessages() Function function checkmessages() { vartheMessage = "Invalid Message - Please complete the following: ------------------------------------------------------------------------"; varnoErrors = true; if (document.getElementById("emessage").name.value=="") { theMessage = theMessage + " --> Name"; noErrors = false; } // If no errors, submit the form if (noErrors) { return true; } else { // errors were found, show alert message alert(theMessage); document.getElementById("emessage").submit.disabled=true; return false; } } // end of function checkmessages() This is the code that checks the name field. To check the remaining fields you only need copy and paste this and edit the name of the field being checked. For the select field however you will need to check if the value is "0"
  • 11. Styling the Form To pretty up the form we use a style sheet: <style> label{ float: left; width: 180px; font-weight: bold; } textarea,select,input{ margin-bottom: 5px; } #submit{ margin-left: 200px; margin-top: 5px; width: 100px; } br{ clear: left; } </style>
  • 12. Warning the User If a required field is not filled in we output a warning message to this effect on the screen Let’s assume the name field isn’t filled in.
  • 13. Confirming Form Submission Once the form has been successfully submitted we need to inform the user of this This can be done with a simple php script (see next slide).