This site is mobile accessible. Press the "Tap Here" button to use a larger font-size.
Smartphone icons created by Freepik - Flaticon
14.6 DOM Forms
JavaScript Form Validation
- HTML form validation can be done by JavaScript.
- If a form field (fname) is empty, this function alerts a message, and returns false, to prevent the form from being submitted:
JavaScript Example
function validateForm() {
let x = document.forms["myForm"]["fname"].value;
if (x == "") {
alert("Name must be filled out");
return false;
}
}
The function can be called when the form is submitted:
<form name="myForm" action="/action_page.php" onsubmit="return validateForm()" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
JavaScript Can Validate Numeric Input
JavaScript is often used to validate numeric input:
- Please input a number between 1 and 10
- Submit
Try it yourself
Automatic HTML Form Validation
- HTML form validation can be performed automatically by the browser:
- If a form field (fname) is empty, the required attribute prevents this form from being submitted:
<form action="/action_page.php" method="post">
<input type="text" name="fname" required>
<input type="submit" value="Submit">
</form>
Automatic HTML form validation does not work in Internet Explorer 9 or earlier.
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
Constraint Validation HTML Input Attributes
Attribute |
Description |
disabled |
Specifies that the input element should be disabled |
max |
Specifies the maximum value of an input element |
min |
Specifies the minimum value of an input element |
pattern |
Specifies the value pattern of an input element |
required |
Specifies that the input field requires an element |
type |
Specifies the type of an input element |
For a full list, go to HTML Input Attributes.
Constraint Validation CSS Pseudo Selectors
Selector |
Description |
:disabled |
Selects input elements with the "disabled" attribute specified |
:invalid |
Selects input elements with invalid values |
:optional |
Selects input elements with no "required" attribute specified |
:required |
Selects input elements with the "required" attribute specified |
:valid |
Selects input elements with valid values |
For a full list, go to CSS Pseudo Classes.
JavaScript icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon
Example files created in this module:
HTML DOM Methods
JS HTML DOM - Finding HTML Element by Id
JS HTML DOM - Finding HTML Elements by Tag Name part 1
JS HTML DOM - Finding HTML Elements by Tag Name part 2
JS HTML DOM - Finding HTML Elements by Class Name
JS HTML DOM - Finding HTML Elements by CSS Selectors
JS HTML DOM - Finding HTML Elements by HTML document.forms
JS Changing HTML Content part 1
JS Changing HTML Content part 2
JS HTML DOM - Changing the Value of an Attribute
JS HTML DOM - Dynamic HTML content
JS HTML DOM - document.write()
JS Validation - HTML Form Example part 1
JS Validation - HTML Form Example part 2
JS HTML DOM - Changing the HTML style
JS HTML DOM - Changing the HTML style Using Events
My first JS animation part 1
My first JS animation part 2
JS HTML Events - The onclick Attribute part 1
JS HTML Events - The onclick Attribute part 2
JS HTML Events - The onclick Attribute part 3
JS HTML Events - The onclick Attribute part 4
JS HTML Events - The onload Attribute
JS addEventListener() part 1
JS addEventListener() part 2
JS addEventListener() part 3
JS addEventListener() part 4
JS addEventListener() part 5
JS addEventListener() part 6
JS addEventListener() part 7
JS addEventListener() part 8
JS removeEventListener()
JS HTML Child Nodes and Node Values part 1
JS HTML Child Nodes and Node Values part 2
JS HTML Child Nodes and Node Values part 3
JS HTMLDOM - DOM Root Nodes part 1
JS HTMLDOM - DOM Root Nodes part 2
JS HTMLDOM - The nodeName Property
JS HTMLDOM - The nodeType Property
JS HTML DOM - Creating New HTML Elements (Nodes)
JS HTML DOM - Creating new HTML Elements - insertBefore()
JS HTML DOM - Removing Existing HTML Elements
JS HTML DOM - Remove Child Element
JS HTML DOM - Replacing an HTML Elements
JS HTML DOM Collections
JS HTML DOM Collections - HTMLCollection Length par 1
JS HTML DOM Collections - HTMLCollection Length part 2
JS HTML DOM - DOM Node Lists
JS HTML DOM - DOM Node List Length part 1
JS HTML DOM - DOM Node List Length part 2