JavaScript verifies the entire form

Source: Internet
Author: User
Tags html form numeric numeric value valid domain
Javascript

Unlike domain-level validation (field-level validation), a form-level validation check (form-level validation) analyzes the consistency of a group (or all) of values on the entire form as a whole. A form-level validation check typically occurs before a completed HTML form is submitted to a CGI program. We do this to ensure that all required fields are filled out before the user sends the data to the server.

Validating the entire form is actually fairly straightforward. In our example, we have gone beyond the most domain-level validation checks that automatically pop up instant warning messages. Here is an example:

function Isanumber (number) {

Answer = 1;

if (!parsefloat (number)) {

The digit wasn ' t numeric

Answer = 0;

} else {

The "the" digit was numeric and so check the rest

for (vari=0 I

if ((Number.charat (i)!= "0")

&& (!parsefloat (Number.charat (i))) {

Answer = 0;

Break

}

}

}

if (answer = = 1) {

orderplaced = true;

}

return answer;

}

The above code, basically, is the number check function in front of us, except that there is no JavaScript warning message. In this case, if the user enters a character other than the number, we do not automatically send the error message.

Once the user thinks she has completed the entire form, she can press the submit button. At that time, we checked each domain for omissions or had malformed data.

function Validateform () {

Varfixthis = "";

If

(! (Isanumber (Document.orderForm.numberOrdered.value)) {

Fixthis + = "Please enter a numeric value

For the number of brains field.\n ";

}

If

(! (Exists (Document.orderForm.typeField.value)) {

Fixthis + = "Please enter the type.\n";

}

If

(! (Exists (Document.orderForm.stateField.value)) {

Fixthis + = "Please enter the state.\n";

}

If

(! (Isaphonenumber (Document.orderForm.phoneNumber.value)) {

Fixthis + = "Please enter the phone number

In the following format: (123) 456-7890 ";

}

If

(Fixthis!= "") {

alert (fixthis);

} else {

Document.location.href = "thanks.html";

}

}

This function checks all the fields in the form to make sure that each field contains a valid value. If it finds that a field lacks valid data, it adds a new warning message to the fixthis variable, and then it goes on. In the end, it either pops up a window containing a variety of warning messages, or sends a short "Thank" to the user.

Note: This example examines some of the--state boxes that we did not mention in the form, which calculates the sales tax based on the encoding of the U.S. states that the user has entered.



Related Article

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.