...

View Full Version : Validation Without alert()



7079
12-13-2006, 12:16 PM
Hi,

I have the following code somewhat working:



function customerAdd(form){
if (form.email.value == ""){
var email = document.getElementById("email");
email.style.color = "red";
}
if(form.isActive[0].checked == false && form.isActive[1].checked == false){
var isActive = document.getElementById("isActive");
isActive.style.color = "red";
}
if(form.receiveEmails[0].checked == false && form.receiveEmails[1].checked == false){
var receiveEmails = document.getElementById("receiveEmails");
receiveEmails.style.color = "red";
}
if (form.salutation.value == "" || form.salutation.value == "- Please Select -"){
var salutation = document.getElementById("salutation");
salutation.style.color = "red";
}
return false;
}


What I meant by somewhat working was that the code actually changes the color of the selected "id" when the fields are empty or have not been selected, but when all the required fields are selected the form doesn't do anything. I knwo it has to do with the return false;

How can I get this to work so that if the user doesn't fill out the required fields they would be highlighted and if everything is filled out the form will submit? I had the code working before with alert boxes, but I wanted to do something different with the way the validation worked rather than having the alert box pop up for each missed field.

I appreciate any and all help!

7079
12-13-2006, 12:19 PM
I also wanted to know how can you check to see if a form field is in the form. I have several pages of forms for users to enter data and I wanted to reuse the same validation code for all my pages rather than creating separate functions for each page.

Let's say based on the 4 form fields on my previous post, page1.cfm uses all 4 fields and page2.cfm only uses 2 of the fields, how can i use that same validation for both pages without having to create separate functions for each page?

_Aerospace_Eng_
12-13-2006, 02:35 PM
Post the html you are using please.

david_kw
12-13-2006, 05:34 PM
I knwo it has to do with the return false;


As Aero said, without seeing your html it is guessing, but maybe try this for your first question.



function customerAdd(form){
var noErrors = true;
if (form.email.value == ""){
var email = document.getElementById("email");
email.style.color = "red";
noErrors = false;
}
if(form.isActive[0].checked == false && form.isActive[1].checked == false){
var isActive = document.getElementById("isActive");
isActive.style.color = "red";
noErrors = false;
}
if(form.receiveEmails[0].checked == false && form.receiveEmails[1].checked == false){
var receiveEmails = document.getElementById("receiveEmails");
receiveEmails.style.color = "red";
noErrors = false;
}
if (form.salutation.value == "" || form.salutation.value == "- Please Select -"){
var salutation = document.getElementById("salutation");
salutation.style.color = "red";
noErrors = false;
}
return noErrors;
}


david_kw



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum