E07 New
E07 New
Experiment No. 07
Title: Write a JavaScript for voter registration form validation at client side.
Objective: To study HTML Document Object Model and JavaScript form validation.
Theory:
What is the DOM?
The Document Object Model (DOM) is a W3C (World Wide Web Consortium) standard
The DOM defines a standard for accessing documents:
"The W3C Document Object Model (DOM) is a platform and language-neutral interface that
allows programs and scripts to dynamically access and update the content, structure, and style of
a document."
The HTML DOM is a standard object model and programming interface for HTML.
It defines:
The HTML elements as objects
The properties of all HTML elements
The methods to access all HTML elements
The events for all HTML elements
In other words: The HTML DOM is a standard for how to get, change, add, or delete HTML
elements.
When a web page is loaded, the browser creates a Document Object Model of the page.
The HTML DOM model is constructed as a tree of Objects:
The HTML DOM Tree of Objects
T.Y.B.Tech (C.S.E.)-II Subject: Web Technologies
With the HTML DOM, JavaScript can access and change all the elements of an HTML document.
With the object model, JavaScript gets all the power it needs to create dynamic HTML:
JavaScript can change all the HTML elements in the page
JavaScript can change all the HTML attributes in the page
JavaScript can change all the CSS styles in the page
JavaScript can remove existing HTML elements and attributes
JavaScript can add new HTML elements and attributes
JavaScript can react to all existing HTML events in the page
JavaScript can create new HTML events in the page
HTML form validation can be done by JavaScript. If a form field is empty, this function alerts a
message, and returns false, to prevent the form from being submitted.
<!DOCTYPE html>
<html>
<head>
<script>
function validateForm() {
let x = document.forms["myForm"]["fname"].value;
if (x == "") {
alert("Name must be filled out");
return false;
}
}
</script>
</head>
<body>
<h2>JavaScript Validation</h2>
<form name="myForm" action="/action_page.php" onsubmit="return validateForm()"
method="post">
<input type="text" name="fname" required>
<input type="submit" value="Submit">
</form>
</body>
T.Y.B.Tech (C.S.E.)-II Subject: Web Technologies
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Validation</h2>
<form action="/action_page.php" method="post">
<input type="text" name="fname" required>
<input type="submit" value="Submit">
</form>
<p>If you click submit, without filling out the text field, your browser will display an
error
message.
</p>
</body>
</html>
Data Validation
Data validation is the process of ensuring that user input is clean, correct, and useful.
Typical validation tasks are:
Has the user filled in all required fields?
Has the user entered a valid date?
Has the user entered text in a numeric field?
Most often, the purpose of data validation is to ensure correct user input.
Validation can be defined by many different methods, and deployed in many different ways.
Server side validation is performed by a web server, after input has been sent to the
server.
Client side validation is performed by a web browser, before input is sent to a web
server.
HTML Constraint Validation
HTML5 introduced a new HTML validation concept called constraint validation.
HTML constraint validation is based on:
Constraint validation HTML Input Attributes
Constraint validation CSS Pseudo Selectors
Constraint validation DOM Properties and Methods
T.Y.B.Tech (C.S.E.)-II Subject: Web Technologies
Algorithm:
1. Design voter registration html form with multiple textbox and a submit button.
2. The data entered by user include Name, DOB, Adhar Number, E-mail ID, Address etc.
3. Validate the entered data at client side using javascript.
4. Display Result