JavaScript Form Validation
Form validation is the process of ensuring that the data entered by the user conforms to the expected format and rules.
In Web development, form validation is commonly used to check the validity of user input, such as email addresses, passwords, phone numbers, etc.
Through form validation, invalid data can be prevented from being submitted to the server, thereby improving data accuracy and security.
JavaScript Form Validation
JavaScript can be used to validate these input data in an HTML form before the data is sent to the server.
Form data often needs to be validated for correctness using JavaScript:
- Check whether the form data is empty?
- Check whether the input is a valid email address?
- Check whether the date is entered correctly?
- Check whether the form input is numeric?
Why Is Form Validation Needed?
- Data Accuracy: Ensure that the data entered by the user conforms to the expected format and avoids invalid data.
- Safety: Prevent malicious users from submitting harmful data, such as SQL injection, cross-site scripting (XSS), etc.
- User experience: Provide timely feedback on user input errors to help users quickly correct problems.
Basic Methods of JavaScript Form Validation
JavaScript provides several methods to implement form validation. The following are a few common approaches:
1. Using Built-in HTML5 Validation
HTML5 provides some built-in form validation features, such as required、pattern、min、max and other attributes. These attributes can easily implement basic form validation.
Example
In the example above,required The attribute ensures that the user must enter an email address, while type="email" automatically verifies whether the input is in a valid email format.
2. Using Custom JavaScript Validation
The following function checks whether the user has filled in the required (or mandatory) items in the form. If a required or mandatory item is empty, an alert box will pop up and the function returns false; otherwise the function returns true (meaning the data is fine):
function
validateForm
(
)
{
var
x
=
document
.
forms
[
"
myForm
"
]
[
"
fname
"
]
.
value
;
if
(
x
==
null
||
x
==
"
"
)
{
alert
(
"
Last name must be filled in
"
)
;
return
false
;
}
}
The above function is called when the form is submitted:
Example
<form name="myForm" action="demo-form.php" onsubmit=">return validateForm()
"
method
=
"
post
"
Last name:
<input type="text" name="fname">
<input type="submit" value="Submit" form>Try it »
E-mail Validation
The following function checks whether the input data conforms to the basic syntax of an email address.
This means the input data must contain the @ symbol and a dot (.). Also, @ cannot be the first character of the email address, and there must be at least one dot after @:
function
validateForm
(
)
{
var
x
=
document
.
forms
[
"
myForm
"
]
[
"
"
]
.
value
;
var
atpos
=
x
.
indexOf
(
"
@
"
)
;
var
dotpos
=
x
.
lastIndexOf
(
"
.
"
)
;
if
(
atpos
<
1
||
dotpos
<
atpos
2
||
dotpos
2
=
x.
length
)
{
alert
(
"
is not a valid e-mail address
"
)
;
return
false
;
}
}
Below is the complete code together with the HTML form:
Example
<form name="myForm" action="demo-form.php" onsubmit=">return validateForm();
"
method
=
"
post
"
Email:
<input type="text" name="email">
<input type="submit" value="Submit" form>Try it »
Validation Using Regular Expressions
Regular Expression (Regular Expression) is a powerful tool that can be used to match complex string patterns.
The following is an example of using a regular expression to validate an email address:
Example
function
validateEmail
(
)
{
var
regex
=
/^[^\s@]+@[^\s@]+.[^\s@]+$/
;
return
regex.
test
(
)
;
}
var
=
"example@example.com"
;
if
(
validateEmail
(
)
)
{
console.
log
(
'Valid email address.'
)
;
}
else
{
console.
log
(
'Invalid email address.'
)
;
}
Try it »
In this example, we define a regular expression to validate the format of an email address. If the entered email address meets the format requirements, the function returns true; otherwise, returns false。