There are two ways you can validate data from a ASP based form. Either you can use the next page to identify the error and then send the user back to the form page where you should let the user know which field he got it wrong, OR you can use JavaScript to valida your form on submittion.
Here, lemme give you an example based on your simple form:
Code:
function validateForm(form) {
this.form = form;
errors = 0
/* ------------------------------------------
form contact
------------------------------------------ */
if (form == "contact") {
form = document.contact;
/* email*/
if (errors== 0) {
Ctrl = form.email;
if ((Ctrl.value == "" || Ctrl.value.indexOf('@', 0) == -1) || Ctrl.value.indexOf('.') == -1) {
validatePrompt(Ctrl, "Please, type in a valid e-mail adress.");
errors++;
}
}
/* name */
if (errors == 0) {
Ctrl = form.name;
if (Ctrl.value == "") {
validatePrompt(Ctrl, "Please, type in your name.");
errors++;
}
}
/* comments */
if (errors == 0) {
Ctrl = form.comments;
if (Ctrl.value == "") {
validatePrompt(Ctrl, "Please, type in your comments.");
errors++;
}
}
/* ------------------------------------------
true or false
------------------------------------------ */
if (errors > 0) {
return false;
} else return true
}
/* ------------------------------------------
Focus and Error Alert
------------------------------------------ */
function validatePrompt(Ctrl, PromptStr) {
alert (PromptStr);
if (Ctrl == "NO") {
return;
} else {
Ctrl.focus();
return;
}
}
// -->
Name your form "contact", and make sure you name your fields correctly too.
Also, notice that you may use "NO" instead of Ctrl in the ValidatePrompt Function if you don't want this script to focus on a certain field (like, you wouldn't want to do that if the field were a radio...)
Your form tag should look like this:
Code:
<form name="contact" action="next_asp_file.asp" method="post" target="if any" onSubmit="return validateForm(this.name);" >
Lemme know if this helped.