JavaScript can be used to verify the input data in the HTML form before the data is sent to the server. The typical form data verified by JavaScript includes:
Have you entered required items in the form?
Is the email address entered by the user valid?
Have you entered a valid date?
Does the user input text in the data field (numeric field?
Required (or required) project
The following function is used to check whether the user has entered a required (or required) project in the form. If required or required, the warning box is displayed, and the return value of the function is false. Otherwise, the return value of the function is true (meaning there is no data problem ):
The Code is as follows:
Function validate_required (field, alerttxt)
{
With (field)
{
If (value = null | value = "")
{Alert (alerttxt); return false}
Else {return true}
}
}
Email verification (verify email)
The following function checks whether the input data conforms to the basic syntax of the email address.
That is to say, the input data must contain the @ symbol and the dot (.). At the same time, @ cannot be the first character of the email address, and @ must have at least one dot:
The Code is 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:
The Code is as follows: