Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript validation exception

Status
Not open for further replies.

abbottboi

IS-IT--Management
Nov 14, 2005
94
CA
Hello Smart people of the world!

I have a simple web form with a seperate javascript validation that validates all fields to be filled in. I have one exception though that is if the user select this radio buttion I want it to skip over 4 of the fields that are supposed to be validated.

Something like IF user select *member, then don't validate the credit section (4 fields) the radio button's name is called 'Member'

Any quick fixes would be awesome!

THANKS!

--see the code below


var errorMsg;

function validate(theform) {
var errorOccurred = false;
errorMsg = "";

if (!theform.Last_name.value) {
errorMsg += "<li>Your last name<" + "/li>";
errorOccurred = true;
}

if (!theform.First_name.value) {
errorMsg += "<li>Your first name<" + "/li>";
errorOccurred = true;
}

if (!theform.Faculty.value) {
errorMsg += "<li>Your faculty studied in<" + "/li>";
errorOccurred = true;
}

if (!theform.Gr_year.value) {
errorMsg += "<li>Your year of graduation<" + "/li>";
errorOccurred = true;
}
if (!theform.Years_at_address1.value) {
errorMsg += "<li>Years living at home address<" + "/li>";
errorOccurred = true;
}
if (!theform.Address1.value) {
errorMsg += "<li>Your street address<" + "/li>";
errorOccurred = true;
}

if (!theform.City.value) {
errorMsg += "<li>Your city<" + "/li>";
errorOccurred = true;
}

if (!theform.Province.value) {
errorMsg += "<li>Your province/state (if applicable)<" + "/li>";
}

if (!theform.Postal_Code.value) {
errorMsg += "<li>Your postal or ZIP code (if applicable)<" + "/li>";
}

if (!theform.Country.value) {
errorMsg += "<li>Your country<" + "/li>";
errorOccurred = true;
}

if (!theform.Home_phone.value) {
errorMsg += "<li>Your home phone number<" + "/li>";
errorOccurred = true;
}

if (!theform.Email.value) {
errorMsg += "<li>Your preferred email address<" + "/li>";
errorOccurred = true;
}

if (!theform.Gift_amount.value) {
errorMsg += "<li>Your gift amount<" + "/li>";
errorOccurred = true;
}

if (!theform.Currency.value) {
errorMsg += "<li>Type of currency<" + "/li>";
errorOccurred = true;
}

if (!theform.Card_holdername.value) {
errorMsg += "<li>Card holder's name<" + "/li>";
errorOccurred = true;
}

if (!theform.Card_type.value) {
errorMsg += "<li>Card type<" + "/li>";
errorOccurred = true;
}

if (!theform.Card_number.value) {
errorMsg += "<li>Card number<" + "/li>";
errorOccurred = true;
}

if (!theform.Card_month.value) {
errorMsg += "<li>Card month expiration<" + "/li>";
errorOccurred = true;
}

if (!theform.Card_year.value) {
errorMsg += "<li>Card year expiration<" + "/li>";
errorOccurred = true;
}

if (errorOccurred) {
errorWindow = window.open("formerror.shtm", "errWin", "width=400,height=275,resizable=yes,scrollbars=yes");
errorWindow.focus();
return false;
}

else return true;
}
 
I'm not sure of the name of the radio group or the position of the member/guest - I guessed that the guest was the second option (hence [1])

Code:
function validate(theform) {
	errorMsg = "";
	
	if (theform.Last_name.value == "") errorMsg += "\nYour last name"
	if (theform.First_name.value) errorMsg += "\nYour first name"
	if (theform.Faculty.value)errorMsg += "\nYour faculty studied in";
	if (theform.Gr_year.value)errorMsg += "\nYour year of graduation";
	if (!theform.Years_at_address1.value)errorMsg += "\nYears living at home address";
	if (!theform.Address1.value)errorMsg += "\nYour street address";
	if (!theform.City.value)errorMsg += "\nYour city";
	//if (!theform.Province.value)errorMsg += "\nYour province/state (if applicable)";
	//if (!theform.Postal_Code.value)errorMsg += "\nYour postal or ZIP code (if applicable)";
	if (!theform.Country.value)errorMsg += "\nYour country";
	if (!theform.Home_phone.value)errorMsg += "\nYour home phone number";
	if (!theform.Email.value)errorMsg += "\nYour preferred email address";
	if (!theform.Gift_amount.value)errorMsg += "\nYour gift amount";
	if (!theform.Currency.value)errorMsg += "\nType of currency";
	if (!theform.Card_holdername.value)errorMsg += "\nCard holder's name";

	if (theForm.Card_type.radioGroupName[1]){
		if (!theform.Card_type.value)errorMsg += "\nCard type";
		if (!theform.Card_number.value)errorMsg += "\nCard number";
		if (!theform.Card_month.value)errorMsg += "\nCard month expiration";
		if (!theform.Card_year.value)errorMsg += "\nCard year expiration";
	}
	
	
	if (errorMsg != "") {
		alert("Please correct the following:\n" + errorMsg)
		return false;
	}
	
	else return true;
}

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
Ooops - forgot to finish the mods...
Code:
function validate(theform) {
	errorMsg = "";
	
	if (theform.Last_name.value == "") errorMsg += "\nYour last name"
	if (theform.First_name.value == "") errorMsg += "\nYour first name"
	if (theform.Faculty.value == "")errorMsg += "\nYour faculty studied in";
	if (theform.Gr_year.value == "")errorMsg += "\nYour year of graduation";
	if (theform.Years_at_address1.value == "")errorMsg += "\nYears living at home address";
	if (theform.Address1.value == "")errorMsg += "\nYour street address";
	if (theform.City.value == "")errorMsg += "\nYour city";
	//if (theform.Province.value == "")errorMsg += "\nYour province/state (if applicable)";
	//if (theform.Postal_Code.value == "")errorMsg += "\nYour postal or ZIP code (if applicable)";
	if (theform.Country.value == "")errorMsg += "\nYour country";
	if (theform.Home_phone.value == "")errorMsg += "\nYour home phone number";
	if (theform.Email.value == "")errorMsg += "\nYour preferred email address";
	if (theform.Gift_amount.value == "")errorMsg += "\nYour gift amount";
	if (theform.Currency.value == "")errorMsg += "\nType of currency";
	if (theform.Card_holdername.value == "")errorMsg += "\nCard holder's name";

	if (theForm.Card_type.radioGroupName[1]){
		if (theform.Card_type.value)errorMsg += "\nCard type";
		if (theform.Card_number.value)errorMsg += "\nCard number";
		if (theform.Card_month.value)errorMsg += "\nCard month expiration";
		if (theform.Card_year.value)errorMsg += "\nCard year expiration";
	}
	
	
	if (errorMsg == "") {
		alert("Please correct the following:\n" + errorMsg)
		return false;
	}
	
	else return true;
}

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
Wow - one of those days...

it should read
Code:
if (errorMsg != "") {
        alert("Please correct the following:\n" + errorMsg)
        return false;
    }

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
Thanks a bunch.. but i want it to skip the Credit card info if the person selects the following radio button.

<input name="Gifts" type="radio" value="payroll-deduction">

group name being "Gifts"

if they select any other radio button selection then the credit card stuff needs to be validated.

thanks for your time.
 
Code:
function validate(theform) {
    errorMsg = "";
    
    if (theform.Last_name.value == "") errorMsg += "\nYour last name"
    if (theform.First_name.value == "") errorMsg += "\nYour first name"
    if (theform.Faculty.value == "")errorMsg += "\nYour faculty studied in";
    if (theform.Gr_year.value == "")errorMsg += "\nYour year of graduation";
    if (theform.Years_at_address1.value == "")errorMsg += "\nYears living at home address";
    if (theform.Address1.value == "")errorMsg += "\nYour street address";
    if (theform.City.value == "")errorMsg += "\nYour city";
    //if (theform.Province.value == "")errorMsg += "\nYour province/state (if applicable)";
    //if (theform.Postal_Code.value == "")errorMsg += "\nYour postal or ZIP code (if applicable)";
    if (theform.Country.value == "")errorMsg += "\nYour country";
    if (theform.Home_phone.value == "")errorMsg += "\nYour home phone number";
    if (theform.Email.value == "")errorMsg += "\nYour preferred email address";
    if (theform.Gift_amount.value == "")errorMsg += "\nYour gift amount";
    if (theform.Currency.value == "")errorMsg += "\nType of currency";
    if (theform.Card_holdername.value == "")errorMsg += "\nCard holder's name";

   //see which radio is selected
   checkCard = true
   for(x=0; x<theForm.Gifts.length; x++){
   	if (theForm.Gifts[x].checked && theForm.Gifts[x].value == "payroll-deduction") checkCard = false
   }
   
    if (checkCard){
        if (theform.Card_type.value)errorMsg += "\nCard type";
        if (theform.Card_number.value)errorMsg += "\nCard number";
        if (theform.Card_month.value)errorMsg += "\nCard month expiration";
        if (theform.Card_year.value)errorMsg += "\nCard year expiration";
    }
    
    
    if (errorMsg != "") {
        alert("Please correct the following:\n" + errorMsg)
        return false;
    }
    
    else return true;
}

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
This validation doesn't work :(

The form is submitted without the validation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top