The typical form data that is validated by JavaScript is:
Has the user filled out the required items in the form?
is the e-mail address that the user entered legal?
Has the user entered a valid date?
Does the user enter text in the Data field (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 a required or required option is blank, the warning box pops 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):
Copy Code code as follows:
function validate_required (field,alerttxt)
{
With (field)
{
if (value==null| | value== "")
{alert (alerttxt); return false}
else {return true}
}
}
E-Mail verification (verify mailbox)
The following function checks whether the data entered conforms to the basic syntax of the e-mail address.
This means that the data you enter must contain the @ symbol and the point number (.). At the same time, @ can not be the first character of the mail address, and after @ must have at least one point number:
Copy Code code as follows:
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}
}
}
Instance:
Copy Code code as follows:
<script type= "Text/javascript" >
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}
}
}
function Validate_form (thisform)
{
With (Thisform)
{
if (validate_email (email, "Not a valid e-mail address!") ==false)
{Email.focus (); return false}
}
}
</script>
<body>
<form action= "submitpage.htm" onsubmit= "return Validate_form (this);" method= "POST" >
Email: <input type= "text" name= "email" size= ">"
<input type= "Submit" value= "Submit" >
</form>
</body>