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.