SlideShare a Scribd company logo
Form Validation in JavaScript
ADMEC MULTIMEDIA
Leader in Animation & Digital Media Education
ISO 9001:2008 CERTIFIED | ADOBE Testing Center
www.admecindia.co.in
Hi, I am Abhishek Ranjan a web designer working in a
company in Delhi and studying UI development and
Responsive Website designing in ADMEC Multimedia
Institute institute to excel my knowledge of JavaScript,
jQuery, and Bootstrap to the advanced level.
What is Form Validation?
Form validation is the process of checking that a form has
been filled in correctly before it is processed, this is
called form validation.
What you need to know before from validation?
•HTML
•CSS
•JavaScript
Note:- If you don't have knowledge of HTML, CSS & JavaScript than you may face some
difficulties in validating a HTML form yet I will make the entire step very easy to understand by
you.
Ok let’s start form Validation
There are main two methods of validating form:
1. Server side method 2. Client side method
1. Server-side: - This validation is more secure
but often more tricky to code because it takes
down theclient side validation. It is important
when a hacker disables the performance of
the website. It is compulsory to add in a web
form but along with the JavaScript then server
side validation protects your website.
2. Client-side : Client side(JavaScript) validation
is easier to do and quicker too (the browser
doesn't have to connect to the server to
validate the form, so the user finds out instant
ly if they've missed out that required field!).
JavaScript provides a way to validate form's data on the client's computer before
sending it to the web server. Form validation generally performs two functions.
 Basic Validation - First of all, the form must be checked to make sure data was
entered into each form field that required it. This would need just loop through each
field in the form and check for data.
 Data Format Validation - Secondly, the data that is entered must be checked for
correct form and value. This would need to put more logic to test correctness of data.
A Simple Form with Validation :
Let's build a simple form with a validation script. The form will include more than one
input box, select-box, checkbox, radio-button,
drop-down-box, message-box, reset-button and a submit button. Our validation
script will ensure that the users enter their proper
information before the form is sent to the server.
Example:
HTML form:
//Below I am using these methods, action, onsubmit="return validation(this) in HTML first line
which is necessary for Form:
Ok now,
First tell you what is a method?
Methods are the action that can be performed on objects through which a browser sends form
information to Server.
There are two types of methods:-
1. Post 2. Get
Post method : Post method is used for sending information to server. All programmers use
mainly post method because it is secure and can transfer unlimited information.
Get method : Get method is used for sending information to server too. Get method is less-
secure than Post method.
Action: Action is an form attribute where we assign a PHP file generally and this file capture
the data submitted from this form and then validate it for the further uses.
onsubmit : onsubmit is an event of JavaScript, it monitors for the submit action as someone
clicks on
<form method="post" action="http://www.admecindia.co.in" onsubmit="return
validation(this)">
Name:<input type="text" name="name" /><br />
Phone:<input type="text" name="phone" /><br />
Email:<input type="text" name="email" /><br />
Gender:<input type="radio" value="male" name="gender" >Male
<input type="radio" value="female" name="gender" >Female<br />
Reference:<input type="checkbox" value="tv" name="tv" >TV
<input type="checkbox" value="radio" name="radio" >Radio<br />
Course: <select name="course">
<option value="">Select Please</option>
<option value="Graphic">Graphic</option>
<option value="Web">Web</option>
<option value="Animation">Animation</option>
</select><br />
Comments: <textarea rows="5" cols="10" name="comm"></textarea><br />
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
JavaScript:
Now I am going to create a function with the name of validation and it has a parameter in it.
This parameter will be replace with ‘this’ keyword in form’s ‘onsubmit’ attribute so it will
point to the form. I will be collecting all the values first and then add conditions to check one
by one using ‘if’ conditional so that I can show all the errors at a time. You will notice a
regular expression pattern too in this form validation in JavaScript code.
<script type="text/javascript">
//lets create a function
function validation(arg){
// arg is used to pass value
vargName = arg.name.value;
//Get the value of name(selecter) and put in gName(variable)
vargPhone = arg.phone.value;
//Get the value of phone(selecter) and put in gPhone(Variable)
vargEmail = arg.email.value;
//Get the value of email(selecter) and put in gEmail(Variable)
vargComm = arg.comm.value;
//Get the Value of comm(selecter) and put in gComm (Variable)
varePat = /^[a-z-._0-9]+@[a-z0-9]+.[a-z.]{2,5}$/;
/* ^[a-z-._0-9] ->Reg Ex is use to allow use only a-z , . , _ and 0-9 digits.
+@ -> "+" is for concatinet @
[a-z0-9] ->Reg ex is use to allow to use only a-z and 0-9 digits */
varnamePatDt = /./;
//Find a single character, except newline or line terminator
varnamePatDgt = /d/;
//Only digits find
var err = "";
// err variable is used to show error message.
varerrNum = 0;
// errNum variable is used to show error index
if (gName == "" || gName.length< 3 || namePatDt.test(gName) ||
namePatDgt.test(gName)) {
errNum++;
err += errNum + ". Invalid name.n";
}
/* This code check the value : If Value empety or value length less than 3 or value is digit than
Show error Indvalid Name */
if (gPhone == "" || gPhone.length< 8 || isNaN(gPhone)) {
errNum++;
err += errNum + ". Invalid phone num.n";
}
/* This code check the value : If value is empty or value length less than 8 or is not a
null then show error Invalid Phone num */
if (gEmail == "") {
errNum++;
err += errNum + ". Enter Email.n";
}
/* If you don't Enter anything in Email field than show error(Enter Email) */
else{
if(!ePat.test(gEmail)){
errNum++;
err += errNum + ". Invalid Email.n";
}
}
/* If you don't Enter Email address in ePat format (i already discribe "ePat") than shaw
error (Invalid Email) */
if (gComm == "") {
errNum++;
err += errNum + ". Enter comments.n";
}
/* If you don't Enter anything in Comment field than show error(Enter Comments) */
if (!arg.gender[0].checked && !arg.gender[1].checked) {
errNum++;
err += errNum + ". Select gender.n";
}
/* If you don't checked 0 index of gender field or 1 index than show error(Select gender)*/
if (!arg.tv.checked&& !arg.radio.checked) {
errNum++;
err += errNum + ". Select Reference.n";
}
/* If you don't checked tv field and radio field than show error(Select Reference) */
if (arg.course.selectedIndex< 1) {
errNum++;
err += errNum + ". Select Course.n";
}
/* This code check your selection index if your index is less than 1 than show error (Select
course) */
if (errNum>0) {
alert(err);
return false;
}
/* IferrNum is greater than 0 than alert error and return "false" */
else{
alert('done');
return true;
}
/* If errNum is less than 0 or 0 than alert "done"
and return "true"*/
}
main();
</script>
Summary :
Above explanation will surely help you in understanding Form Validation in JavaScript and
HTML. The Form Validation is being used almost in every website. It is very good to create a
safe and performance rich website because everything can't be left out for the server as sever
sucks. If you want help or advanced Form Validation training then you can join our online
JavaScript classes or classroom.
Contact Us:
ADMEC MULTIMEDIA INSTITUTE
C-7/114, IInd Floor, Sector- 7, Rohini, Delhi- 85
Landmark: Near Rohini East Metro Station
Helpline 1: +91 9811 818 122
Helpline 2: +91 9911 782 350
ADMEC MULTIMEDIA
Leader in Animation & Digital Media Education
ISO 9001:2008 CERTIFIED | ADOBE Testing Center
ADMEC MULTIMEDIA INSTITUTE
For More information you can visit :
http://www.admecindia.co.in
OR
http://www.admecindia.co.in/blog/easy-tutorial-html-form-validation-using-javascript

More Related Content

What's hot

Scripting languages
Scripting languagesScripting languages
Scripting languages
teach4uin
 
Asp.NET Validation controls
Asp.NET Validation controlsAsp.NET Validation controls
Asp.NET Validation controls
Guddu gupta
 
Ajax ppt
Ajax pptAjax ppt
JavaScript - Chapter 3 - Introduction
 JavaScript - Chapter 3 - Introduction JavaScript - Chapter 3 - Introduction
JavaScript - Chapter 3 - Introduction
WebStackAcademy
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
Arulmurugan Rajaraman
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
Priya Goyal
 
Html frames
Html framesHtml frames
Html frames
eShikshak
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
Walid Ashraf
 
Statements and Conditions in PHP
Statements and Conditions in PHPStatements and Conditions in PHP
Statements and Conditions in PHP
Maruf Abdullah (Rion)
 
Php string function
Php string function Php string function
Php string function
Ravi Bhadauria
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypesVarun C M
 
Functions in javascript
Functions in javascriptFunctions in 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
Maitree Patel
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
Amit Tyagi
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state management
priya Nithya
 
Bootstrap
BootstrapBootstrap
Bootstrap
Jadson Santos
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - Operators
WebStackAcademy
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
Bala Narayanan
 

What's hot (20)

Scripting languages
Scripting languagesScripting languages
Scripting languages
 
Asp.NET Validation controls
Asp.NET Validation controlsAsp.NET Validation controls
Asp.NET Validation controls
 
Ajax ppt
Ajax pptAjax ppt
Ajax ppt
 
JavaScript - Chapter 3 - Introduction
 JavaScript - Chapter 3 - Introduction JavaScript - Chapter 3 - Introduction
JavaScript - Chapter 3 - Introduction
 
Java script
Java scriptJava script
Java script
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 
Html frames
Html framesHtml frames
Html frames
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
 
Statements and Conditions in PHP
Statements and Conditions in PHPStatements and Conditions in PHP
Statements and Conditions in PHP
 
Php string function
Php string function Php string function
Php string function
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
Functions in javascript
Functions in javascriptFunctions in javascript
Functions in 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
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state management
 
Bootstrap
BootstrapBootstrap
Bootstrap
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - Operators
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 

Similar to Form Validation in JavaScript

JavaScript - Chapter 14 - Form Handling
 JavaScript - Chapter 14 - Form Handling   JavaScript - Chapter 14 - Form Handling
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
Wheels
WheelsWheels
Wheels
guest9fd0a95
 
HTML::FormFu talk for Sydney PM
HTML::FormFu talk for Sydney PMHTML::FormFu talk for Sydney PM
HTML::FormFu talk for Sydney PM
Dean Hamstead
 
Clean tests good tests
Clean tests   good testsClean tests   good tests
Clean tests good tests
Shopsys Framework
 
Php forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiPhp forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligeti
Naveen Kumar Veligeti
 
Acceptance Testing With Selenium
Acceptance Testing With SeleniumAcceptance Testing With Selenium
Acceptance Testing With Seleniumelliando dias
 
Cis407 a ilab 5 web application development devry university
Cis407 a ilab 5 web application development devry universityCis407 a ilab 5 web application development devry university
Cis407 a ilab 5 web application development devry universitylhkslkdh89009
 
Html basics 11 form validation
Html basics 11 form validationHtml basics 11 form validation
Html basics 11 form validationH K
 
Php tests tips
Php tests tipsPhp tests tips
Php tests tips
Damian Sromek
 
Html forms
Html formsHtml forms
Html forms
eShikshak
 
Javascript
JavascriptJavascript
Javascript
D V BHASKAR REDDY
 
PHP Form Validation Technique
PHP Form Validation TechniquePHP Form Validation Technique
PHP Form Validation Technique
Morshedul Arefin
 
05 html-forms
05 html-forms05 html-forms
05 html-formsPalakshya
 
Automated Testing Of Web Applications Using XML
Automated  Testing Of  Web  Applications Using  XMLAutomated  Testing Of  Web  Applications Using  XML
Automated Testing Of Web Applications Using XML
diongillard
 
Surviving javascript.pptx
Surviving javascript.pptxSurviving javascript.pptx
Surviving javascript.pptx
Tamas Rev
 

Similar to Form Validation in JavaScript (20)

JavaScript - Chapter 14 - Form Handling
 JavaScript - Chapter 14 - Form Handling   JavaScript - Chapter 14 - Form Handling
JavaScript - Chapter 14 - Form Handling
 
Testing techniques
Testing techniquesTesting techniques
Testing techniques
 
Wheels
WheelsWheels
Wheels
 
HTML::FormFu talk for Sydney PM
HTML::FormFu talk for Sydney PMHTML::FormFu talk for Sydney PM
HTML::FormFu talk for Sydney PM
 
2310 b 07
2310 b 072310 b 07
2310 b 07
 
Clean tests good tests
Clean tests   good testsClean tests   good tests
Clean tests good tests
 
Php forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiPhp forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligeti
 
Acceptance Testing With Selenium
Acceptance Testing With SeleniumAcceptance Testing With Selenium
Acceptance Testing With Selenium
 
Cis407 a ilab 5 web application development devry university
Cis407 a ilab 5 web application development devry universityCis407 a ilab 5 web application development devry university
Cis407 a ilab 5 web application development devry university
 
Html basics 11 form validation
Html basics 11 form validationHtml basics 11 form validation
Html basics 11 form validation
 
Qtp day 3
Qtp day 3Qtp day 3
Qtp day 3
 
Php tests tips
Php tests tipsPhp tests tips
Php tests tips
 
Html forms
Html formsHtml forms
Html forms
 
Javascript
JavascriptJavascript
Javascript
 
PHP Form Validation Technique
PHP Form Validation TechniquePHP Form Validation Technique
PHP Form Validation Technique
 
05 html-forms
05 html-forms05 html-forms
05 html-forms
 
Automated Testing Of Web Applications Using XML
Automated  Testing Of  Web  Applications Using  XMLAutomated  Testing Of  Web  Applications Using  XML
Automated Testing Of Web Applications Using XML
 
Surviving javascript.pptx
Surviving javascript.pptxSurviving javascript.pptx
Surviving javascript.pptx
 
Html forms
Html formsHtml forms
Html forms
 
Testcase
TestcaseTestcase
Testcase
 

More from Ravi Bhadauria

3 Important Terms of Post Production
3 Important Terms of Post Production3 Important Terms of Post Production
3 Important Terms of Post Production
Ravi Bhadauria
 
Basics of Video Editing | Types of Video Editing | Video Production Process
Basics of Video Editing | Types of Video Editing | Video Production ProcessBasics of Video Editing | Types of Video Editing | Video Production Process
Basics of Video Editing | Types of Video Editing | Video Production Process
Ravi Bhadauria
 
Basics of Media | Types of Media | Units in Media | Software in Media | Color...
Basics of Media | Types of Media | Units in Media | Software in Media | Color...Basics of Media | Types of Media | Units in Media | Software in Media | Color...
Basics of Media | Types of Media | Units in Media | Software in Media | Color...
Ravi Bhadauria
 
History of Visual Communication | Guide to Visual Communication by ADMEC Mult...
History of Visual Communication | Guide to Visual Communication by ADMEC Mult...History of Visual Communication | Guide to Visual Communication by ADMEC Mult...
History of Visual Communication | Guide to Visual Communication by ADMEC Mult...
Ravi Bhadauria
 
Elements and Principles of Design (Updated)
Elements and Principles of Design (Updated)Elements and Principles of Design (Updated)
Elements and Principles of Design (Updated)
Ravi Bhadauria
 
Top Graphic Designing Hacks to Make You a Designing Pro Today
Top Graphic Designing Hacks to Make You a Designing Pro Today Top Graphic Designing Hacks to Make You a Designing Pro Today
Top Graphic Designing Hacks to Make You a Designing Pro Today
Ravi Bhadauria
 
12 Famous Typographers to Inspire You
12 Famous Typographers to Inspire You12 Famous Typographers to Inspire You
12 Famous Typographers to Inspire You
Ravi Bhadauria
 
Sargam UI Design
Sargam UI DesignSargam UI Design
Sargam UI Design
Ravi Bhadauria
 
Use of Shapes in Graphic Design | Psychology of Shapes by ADMEC (Updated)
Use of Shapes in Graphic Design | Psychology of Shapes by ADMEC (Updated)Use of Shapes in Graphic Design | Psychology of Shapes by ADMEC (Updated)
Use of Shapes in Graphic Design | Psychology of Shapes by ADMEC (Updated)
Ravi Bhadauria
 
UX Design Essential Theories
UX Design Essential TheoriesUX Design Essential Theories
UX Design Essential Theories
Ravi Bhadauria
 
Top 10 Ad Gurus
Top 10 Ad GurusTop 10 Ad Gurus
Top 10 Ad Gurus
Ravi Bhadauria
 
Workshop on resume, portfolio, interview
Workshop on resume, portfolio, interviewWorkshop on resume, portfolio, interview
Workshop on resume, portfolio, interview
Ravi Bhadauria
 
Top 10 Architecture Design Colleges in India
Top 10 Architecture Design Colleges in IndiaTop 10 Architecture Design Colleges in India
Top 10 Architecture Design Colleges in India
Ravi Bhadauria
 
User interface and user experience ui ux design basics
User interface  and user experience ui ux design basicsUser interface  and user experience ui ux design basics
User interface and user experience ui ux design basics
Ravi Bhadauria
 
How to create Frost Neon Effect in Photoshop?
How to create Frost Neon Effect in Photoshop?How to create Frost Neon Effect in Photoshop?
How to create Frost Neon Effect in Photoshop?
Ravi Bhadauria
 
Top 10 design colleges and institutes of india
Top 10 design colleges and institutes of indiaTop 10 design colleges and institutes of india
Top 10 design colleges and institutes of india
Ravi Bhadauria
 
Best Hollywood poster designers
Best Hollywood poster designersBest Hollywood poster designers
Best Hollywood poster designers
Ravi Bhadauria
 
Design Principles for All the Designers
Design Principles for All the DesignersDesign Principles for All the Designers
Design Principles for All the Designers
Ravi Bhadauria
 
Content Writing Tips for SEO
Content Writing Tips for SEOContent Writing Tips for SEO
Content Writing Tips for SEO
Ravi Bhadauria
 
6 Great Steps to Know to Create Successful Web GUI
6 Great Steps to Know to Create Successful Web GUI6 Great Steps to Know to Create Successful Web GUI
6 Great Steps to Know to Create Successful Web GUI
Ravi Bhadauria
 

More from Ravi Bhadauria (20)

3 Important Terms of Post Production
3 Important Terms of Post Production3 Important Terms of Post Production
3 Important Terms of Post Production
 
Basics of Video Editing | Types of Video Editing | Video Production Process
Basics of Video Editing | Types of Video Editing | Video Production ProcessBasics of Video Editing | Types of Video Editing | Video Production Process
Basics of Video Editing | Types of Video Editing | Video Production Process
 
Basics of Media | Types of Media | Units in Media | Software in Media | Color...
Basics of Media | Types of Media | Units in Media | Software in Media | Color...Basics of Media | Types of Media | Units in Media | Software in Media | Color...
Basics of Media | Types of Media | Units in Media | Software in Media | Color...
 
History of Visual Communication | Guide to Visual Communication by ADMEC Mult...
History of Visual Communication | Guide to Visual Communication by ADMEC Mult...History of Visual Communication | Guide to Visual Communication by ADMEC Mult...
History of Visual Communication | Guide to Visual Communication by ADMEC Mult...
 
Elements and Principles of Design (Updated)
Elements and Principles of Design (Updated)Elements and Principles of Design (Updated)
Elements and Principles of Design (Updated)
 
Top Graphic Designing Hacks to Make You a Designing Pro Today
Top Graphic Designing Hacks to Make You a Designing Pro Today Top Graphic Designing Hacks to Make You a Designing Pro Today
Top Graphic Designing Hacks to Make You a Designing Pro Today
 
12 Famous Typographers to Inspire You
12 Famous Typographers to Inspire You12 Famous Typographers to Inspire You
12 Famous Typographers to Inspire You
 
Sargam UI Design
Sargam UI DesignSargam UI Design
Sargam UI Design
 
Use of Shapes in Graphic Design | Psychology of Shapes by ADMEC (Updated)
Use of Shapes in Graphic Design | Psychology of Shapes by ADMEC (Updated)Use of Shapes in Graphic Design | Psychology of Shapes by ADMEC (Updated)
Use of Shapes in Graphic Design | Psychology of Shapes by ADMEC (Updated)
 
UX Design Essential Theories
UX Design Essential TheoriesUX Design Essential Theories
UX Design Essential Theories
 
Top 10 Ad Gurus
Top 10 Ad GurusTop 10 Ad Gurus
Top 10 Ad Gurus
 
Workshop on resume, portfolio, interview
Workshop on resume, portfolio, interviewWorkshop on resume, portfolio, interview
Workshop on resume, portfolio, interview
 
Top 10 Architecture Design Colleges in India
Top 10 Architecture Design Colleges in IndiaTop 10 Architecture Design Colleges in India
Top 10 Architecture Design Colleges in India
 
User interface and user experience ui ux design basics
User interface  and user experience ui ux design basicsUser interface  and user experience ui ux design basics
User interface and user experience ui ux design basics
 
How to create Frost Neon Effect in Photoshop?
How to create Frost Neon Effect in Photoshop?How to create Frost Neon Effect in Photoshop?
How to create Frost Neon Effect in Photoshop?
 
Top 10 design colleges and institutes of india
Top 10 design colleges and institutes of indiaTop 10 design colleges and institutes of india
Top 10 design colleges and institutes of india
 
Best Hollywood poster designers
Best Hollywood poster designersBest Hollywood poster designers
Best Hollywood poster designers
 
Design Principles for All the Designers
Design Principles for All the DesignersDesign Principles for All the Designers
Design Principles for All the Designers
 
Content Writing Tips for SEO
Content Writing Tips for SEOContent Writing Tips for SEO
Content Writing Tips for SEO
 
6 Great Steps to Know to Create Successful Web GUI
6 Great Steps to Know to Create Successful Web GUI6 Great Steps to Know to Create Successful Web GUI
6 Great Steps to Know to Create Successful Web GUI
 

Recently uploaded

This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
nirahealhty
 
test test test test testtest test testtest test testtest test testtest test ...
test test  test test testtest test testtest test testtest test testtest test ...test test  test test testtest test testtest test testtest test testtest test ...
test test test test testtest test testtest test testtest test testtest test ...
Arif0071
 
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
ufdana
 
Comptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guideComptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guide
GTProductions1
 
BASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptxBASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptx
natyesu
 
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdfJAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
Javier Lasa
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Sanjeev Rampal
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
3ipehhoa
 
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
eutxy
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
JungkooksNonexistent
 
Internet-Security-Safeguarding-Your-Digital-World (1).pptx
Internet-Security-Safeguarding-Your-Digital-World (1).pptxInternet-Security-Safeguarding-Your-Digital-World (1).pptx
Internet-Security-Safeguarding-Your-Digital-World (1).pptx
VivekSinghShekhawat2
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
Rogerio Filho
 
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
3ipehhoa
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
laozhuseo02
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
Gal Baras
 
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
keoku
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
3ipehhoa
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
JeyaPerumal1
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Brad Spiegel Macon GA
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC
 

Recently uploaded (20)

This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
 
test test test test testtest test testtest test testtest test testtest test ...
test test  test test testtest test testtest test testtest test testtest test ...test test  test test testtest test testtest test testtest test testtest test ...
test test test test testtest test testtest test testtest test testtest test ...
 
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
 
Comptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guideComptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guide
 
BASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptxBASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptx
 
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdfJAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
 
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
 
Internet-Security-Safeguarding-Your-Digital-World (1).pptx
Internet-Security-Safeguarding-Your-Digital-World (1).pptxInternet-Security-Safeguarding-Your-Digital-World (1).pptx
Internet-Security-Safeguarding-Your-Digital-World (1).pptx
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
 
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
 
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
 

Form Validation in JavaScript

  • 1. Form Validation in JavaScript ADMEC MULTIMEDIA Leader in Animation & Digital Media Education ISO 9001:2008 CERTIFIED | ADOBE Testing Center www.admecindia.co.in
  • 2. Hi, I am Abhishek Ranjan a web designer working in a company in Delhi and studying UI development and Responsive Website designing in ADMEC Multimedia Institute institute to excel my knowledge of JavaScript, jQuery, and Bootstrap to the advanced level. What is Form Validation? Form validation is the process of checking that a form has been filled in correctly before it is processed, this is called form validation. What you need to know before from validation? •HTML •CSS •JavaScript Note:- If you don't have knowledge of HTML, CSS & JavaScript than you may face some difficulties in validating a HTML form yet I will make the entire step very easy to understand by you.
  • 3. Ok let’s start form Validation There are main two methods of validating form: 1. Server side method 2. Client side method 1. Server-side: - This validation is more secure but often more tricky to code because it takes down theclient side validation. It is important when a hacker disables the performance of the website. It is compulsory to add in a web form but along with the JavaScript then server side validation protects your website. 2. Client-side : Client side(JavaScript) validation is easier to do and quicker too (the browser doesn't have to connect to the server to validate the form, so the user finds out instant ly if they've missed out that required field!).
  • 4. JavaScript provides a way to validate form's data on the client's computer before sending it to the web server. Form validation generally performs two functions.  Basic Validation - First of all, the form must be checked to make sure data was entered into each form field that required it. This would need just loop through each field in the form and check for data.  Data Format Validation - Secondly, the data that is entered must be checked for correct form and value. This would need to put more logic to test correctness of data. A Simple Form with Validation : Let's build a simple form with a validation script. The form will include more than one input box, select-box, checkbox, radio-button, drop-down-box, message-box, reset-button and a submit button. Our validation script will ensure that the users enter their proper information before the form is sent to the server.
  • 5. Example: HTML form: //Below I am using these methods, action, onsubmit="return validation(this) in HTML first line which is necessary for Form: Ok now, First tell you what is a method? Methods are the action that can be performed on objects through which a browser sends form information to Server. There are two types of methods:- 1. Post 2. Get Post method : Post method is used for sending information to server. All programmers use mainly post method because it is secure and can transfer unlimited information. Get method : Get method is used for sending information to server too. Get method is less- secure than Post method. Action: Action is an form attribute where we assign a PHP file generally and this file capture the data submitted from this form and then validate it for the further uses. onsubmit : onsubmit is an event of JavaScript, it monitors for the submit action as someone clicks on
  • 6. <form method="post" action="http://www.admecindia.co.in" onsubmit="return validation(this)"> Name:<input type="text" name="name" /><br /> Phone:<input type="text" name="phone" /><br /> Email:<input type="text" name="email" /><br /> Gender:<input type="radio" value="male" name="gender" >Male <input type="radio" value="female" name="gender" >Female<br /> Reference:<input type="checkbox" value="tv" name="tv" >TV <input type="checkbox" value="radio" name="radio" >Radio<br /> Course: <select name="course"> <option value="">Select Please</option> <option value="Graphic">Graphic</option> <option value="Web">Web</option> <option value="Animation">Animation</option> </select><br /> Comments: <textarea rows="5" cols="10" name="comm"></textarea><br /> <input type="submit" value="Submit"> <input type="reset" value="Reset"> </form>
  • 7. JavaScript: Now I am going to create a function with the name of validation and it has a parameter in it. This parameter will be replace with ‘this’ keyword in form’s ‘onsubmit’ attribute so it will point to the form. I will be collecting all the values first and then add conditions to check one by one using ‘if’ conditional so that I can show all the errors at a time. You will notice a regular expression pattern too in this form validation in JavaScript code. <script type="text/javascript"> //lets create a function function validation(arg){ // arg is used to pass value vargName = arg.name.value; //Get the value of name(selecter) and put in gName(variable) vargPhone = arg.phone.value; //Get the value of phone(selecter) and put in gPhone(Variable) vargEmail = arg.email.value; //Get the value of email(selecter) and put in gEmail(Variable) vargComm = arg.comm.value; //Get the Value of comm(selecter) and put in gComm (Variable) varePat = /^[a-z-._0-9]+@[a-z0-9]+.[a-z.]{2,5}$/; /* ^[a-z-._0-9] ->Reg Ex is use to allow use only a-z , . , _ and 0-9 digits. +@ -> "+" is for concatinet @
  • 8. [a-z0-9] ->Reg ex is use to allow to use only a-z and 0-9 digits */ varnamePatDt = /./; //Find a single character, except newline or line terminator varnamePatDgt = /d/; //Only digits find var err = ""; // err variable is used to show error message. varerrNum = 0; // errNum variable is used to show error index if (gName == "" || gName.length< 3 || namePatDt.test(gName) || namePatDgt.test(gName)) { errNum++; err += errNum + ". Invalid name.n"; } /* This code check the value : If Value empety or value length less than 3 or value is digit than Show error Indvalid Name */ if (gPhone == "" || gPhone.length< 8 || isNaN(gPhone)) { errNum++; err += errNum + ". Invalid phone num.n"; }
  • 9. /* This code check the value : If value is empty or value length less than 8 or is not a null then show error Invalid Phone num */ if (gEmail == "") { errNum++; err += errNum + ". Enter Email.n"; } /* If you don't Enter anything in Email field than show error(Enter Email) */ else{ if(!ePat.test(gEmail)){ errNum++; err += errNum + ". Invalid Email.n"; } } /* If you don't Enter Email address in ePat format (i already discribe "ePat") than shaw error (Invalid Email) */ if (gComm == "") { errNum++; err += errNum + ". Enter comments.n"; }
  • 10. /* If you don't Enter anything in Comment field than show error(Enter Comments) */ if (!arg.gender[0].checked && !arg.gender[1].checked) { errNum++; err += errNum + ". Select gender.n"; } /* If you don't checked 0 index of gender field or 1 index than show error(Select gender)*/ if (!arg.tv.checked&& !arg.radio.checked) { errNum++; err += errNum + ". Select Reference.n"; } /* If you don't checked tv field and radio field than show error(Select Reference) */ if (arg.course.selectedIndex< 1) { errNum++; err += errNum + ". Select Course.n"; } /* This code check your selection index if your index is less than 1 than show error (Select course) */ if (errNum>0) { alert(err); return false; }
  • 11. /* IferrNum is greater than 0 than alert error and return "false" */ else{ alert('done'); return true; } /* If errNum is less than 0 or 0 than alert "done" and return "true"*/ } main(); </script> Summary : Above explanation will surely help you in understanding Form Validation in JavaScript and HTML. The Form Validation is being used almost in every website. It is very good to create a safe and performance rich website because everything can't be left out for the server as sever sucks. If you want help or advanced Form Validation training then you can join our online JavaScript classes or classroom.
  • 12. Contact Us: ADMEC MULTIMEDIA INSTITUTE C-7/114, IInd Floor, Sector- 7, Rohini, Delhi- 85 Landmark: Near Rohini East Metro Station Helpline 1: +91 9811 818 122 Helpline 2: +91 9911 782 350 ADMEC MULTIMEDIA Leader in Animation & Digital Media Education ISO 9001:2008 CERTIFIED | ADOBE Testing Center ADMEC MULTIMEDIA INSTITUTE For More information you can visit : http://www.admecindia.co.in OR http://www.admecindia.co.in/blog/easy-tutorial-html-form-validation-using-javascript