Home  >  Article  >  Web Front-end  >  Detailed explanation of JavaScript form verification (E-mail verification)_javascript skills

Detailed explanation of JavaScript form verification (E-mail verification)_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:07:152485browse

This article shares JavaScript form verification with everyone. The 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?
Did the user enter 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) items in the form. If required or the required field is empty, the warning box will pop up and the return value of the function is false, otherwise the return value of the function 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}
}
}

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>

E-mail verification
The function below checks whether the entered data conforms to the basic syntax of an email address.
This means that the entered 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}
}
}

Here is the complete code along with the HTML form:







Email:

The above is the entire content of this article. I hope it will be helpful for everyone to learn javascript form verification.

Statement:
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