JavaScript Form Verification

Source: Internet
Author: User

JavaScript can be used to verify the input data in the HTML form before the data is sent to the server.

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 a text in the data field (numeric field )?


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

[Html] function validate_required (field, alerttxt)
{
With (field)
{
If (value = null | value = "")
{Alert (alerttxt); return false}
Else {return true}
}
}
Function validate_required (field, alerttxt)
{
With (field)
{
If (value = null | value = "")
{Alert (alerttxt); return false}
Else {return true}
}
}
The following code is used together 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>
<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>

Email Verification
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:

 

[Html] 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_email (field, alerttxt)
{
With (field)
{
Apos = value. indexOf ("@")
Dotpos = value. lastIndexOf (".")
If (apos <1 | dotpos-apos <2)
{Alert (alerttxt); return false}
Else {return true}
}
}
The complete code of the HTML form is as follows:

[Html] <Head>
<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>
</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>

 

 

From sunset hut

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.