JavaScript Forms
JavaScript Form Validation
HTML form validation can be done through JavaScript.
The following example code is used to determine whether the form field (fname) has a value; if not, it pops up a message and prevents form submission:
JavaScript examples
function
validateForm
(
)
{
var
x
=
document
.
forms
[
"
myForm
"
]
[
"
fname
"
]
.
value
;
if
(
x
==
null
||
x
==
"
"
)
{
alert
(
"
A name is required.
"
)
;
return
false
;
}
}
The above JavaScript code can be called via HTML code:
HTML Form Example
<form name="myForm" action="demo_form.php" onsubmit=">return validateForm()
"
method
=
"
post
"
Name: <
input
type
=
"
text
"
name
=
"
fname
"
<input type="submit" value="Submit" form>Try it »
JavaScript validates the entered number
JavaScript is often used to validate entered numbers:
Please enter a number between 1 and 10:
Try it »
HTML form automatic validation
HTML form validation can also be done automatically by the browser.
If the value of the form field (fname) is empty, required the attribute prevents form submission:
Example
<form action="demo_form.php" method="post"> <input type="text" name="fname" required="required"> <input type="submit" value="Submit" form>Try it »
Internet Explorer 9 and earlier IE browsers do not support automatic form validation.
Data validation
Data validation is used to ensure that the data entered by the user is valid.
Typical data validation includes:
- Is a required field filled in?
- Has the user entered valid data?
- Is text entered in a numeric field?
In most cases, data validation is used to ensure that the user enters data correctly.
Data validation can be defined in different ways and invoked in multiple ways.
Server-side data validationis validation performed after the data is submitted to the server.
Client-side data validationis validation completed in the browser before the data is sent to the server.
HTML constraint validation
HTML5 adds a way to validate HTML forms: constraint validation.
Constraint validation is an algorithm the browser uses to perform validation when a form is submitted.
HTML constraint validation is based on:
- HTML input attributes
- CSS pseudo-class selectors
- DOM properties and methods
Constraint validation HTML input attributes
property Description disabled Specifies that the input element is disabled max Specifies the maximum value of the input element min Specifies the minimum value of the input element pattern Specifies the pattern of the input element's value required Specifies that the input element's field is required type Specifies the type of the input element For the full list, see HTML input attributes。
Constraint validation CSS pseudo-class selectors
Selector Description :disabled Selects input elements with the "disabled" attribute :invalid Selects invalid input elements :optional Selects input elements without the "optional" attribute :required Selects input elements with the "required" attribute :valid Selects input elements with valid values For the full list, see CSS pseudo-classes。