Home > Web Front-end > JS Tutorial > body text

JavaScript form validation implementation code_javascript skills

Y2J
Release: 2017-05-23 13:22:41
Original
1695 people have browsed it

This article mainly introduces the implementation code of JavaScript form verification in detail, which has certain reference value. Interested friends can refer to it

JavaScript can be used to process the data before it is sent to the server. Validate these input data in the HTML form

JavaScript Form Validation

JavaScript can be used to validate these input data in the HTML form before the data is sent to the server. Enter data to verify.
These typical form data verified by JavaScript are:
Has the user filled in the required fields in the form?
Is the email address entered by the user legal?
Has the user entered a valid date?
Has the user entered text in the numeric field?

Required (or required) items

The following function is used to check whether the user has filled in the required (or required) fields in the form Select) project. If the required field or required field is empty, then the warning box will pop up, and the function's return value is false, otherwise the function's return value is true (meaning there is no problem with the data) :


function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
 {alert(alerttxt);return false}
else {return true}
}
}
Copy after login

Here is the code along with the HTML form:


<html>
<head>
<script type="text/javascript">

function validate_required(field,alerttxt)
{
with (field)
 {
 if (value==null||value=="")
  {alert(alerttxt);return false}
 else {return true}
 }
}

function validate_form(thisform)
{
with (thisform)
 {
 if (validate_required(email,"Email must be filled out!")==false)
  {email.focus();return false}
 }
}
</script>
</head>

<body>
<form action="submitpage.htm" onsubmit="return validate_form(this)" method="post">
Email: <input type="text" name="email" size="30">
<input type="submit" value="Submit"> 
</form>
</body>

</html>
Copy after login

E-mail Verification

The following function checks whether the entered data conforms to the basic syntax of an email address.
means that the input data must contain the @ symbol and the period (.). At the same time, @ cannot be the first character of the email address, and there must be at least one period after @:


function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@")
dotpos=value.lastIndexOf(".")
if (apos<1||dotpos-apos<2) 
 {alert(alerttxt);return false}
else {return true}
}
}
Copy after login

The following is the complete code together with the HTML form:








Email:
Copy after login

【Related recommendations】

1. Javacript free video tutorial

2. bootstrap modal box Detailed explanation of remote examples

3. Detailed explanation of examples of JS implementing marquee scrolling effect

4. JS production of QQ chat message display and comment submission functions Code example

5. Single line of JS to implement mobile money format verification

The above is the detailed content of JavaScript form validation implementation code_javascript skills. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template