1337hovie 0 Newbie Poster

Hello fellas, new to posting - but a lurker for a definite minute.

I have a working ajax validation function, which works 100%. However, i need to add in 1 extra if statement. I tried writing it, and it's not working. i'll post my working code, and i'll post what i need to get merged. Could be a few issues: 1) I merged it incorrectly, or placed it in the wrong spot. 2) I wrote the additional validation incorrectly.

Basicalyl i need to add (IF formType == "cq_form" make the fields source2, source3 REQUIRED. Seems simple enough, heh.

Working code:

function SchoolFormValidation() {

	formType = $("#thechoices").val();

	var counter = 0;
	var returnvalue = true;

	    if (formType != 'cb_ht' && formType != 'axd_ht') {

		schoolSelect = $('#' + formType).find("select[name^='src']");

		schoolSelect.each(function() {

			// We have a school
			if ($(this).val()) {

				var thisForm = $(this).closest('table');
				var school = thisForm.find("select[name^='cid']");
				var sub = thisForm.find("select[name^='name']");
				
				if (!sub.val()) {
					sub = thisForm.find("input[name^='name']");
				}

				var degree = thisForm.find("input[name^='degree']");
				var prog = thisForm.find("input[name^='program']");

				// Count the Schools
				counter += 1;

				// Validate the fields
				if (!school.val()) {
					sfm_show_error_msg('Please select the school.');
					var targetOffset = school.offset().top;
			    	$('html,body').animate({scrollTop: targetOffset}, 1000);
					returnvalue = false;
					return false;
				}
				else if (!sub.val()) {
					sfm_show_error_msg('Please select/fill in the Sub.');
					var targetOffset = $(this).offset().top;
			    	$('html,body').animate({scrollTop: targetOffset}, 1000);
					returnvalue = false;
					return false;
				}
				else if (!degree.val()) {
					sfm_show_error_msg('Please enter the degree.');
					var targetOffset = degree.offset().top;
			    	$('html,body').animate({scrollTop: targetOffset}, 1000);
					returnvalue = false;
					return false;
				}
				else if (!prog.val()) {
					sfm_show_error_msg('Please enter the program.');
					var targetOffset = prog.offset().top;
			    	$('html,body').animate({scrollTop: targetOffset}, 1000);
					returnvalue = false;
					return false;
				}
			}
		});

		// No Schools
		if (!counter) {
			sfm_show_error_msg('Please select at least one school.');
	    	var targetOffset = $('#boxes').offset().top;
	    	$('html,body').animate({scrollTop: targetOffset}, 1000);
			return false;
		}
	}
	
	return returnvalue;
}

what I wrote to have added into - which doesn't work.

if (formType == 'cq_form') {
  {    
    				if (!source2.val()) { //if CQ form, REQUIRE source2 to have a value
					sfm_show_error_msg('Please select source2');
					var targetOffset = $(this).offset().top;
			    	$('html,body').animate({scrollTop: targetOffset}, 1000);
					returnvalue = false;
					return false;
				}
				else if (!source3.val()) { //if CQ form, REQUIRE source3 to have a value
					sfm_show_error_msg('Please select source3');
					var targetOffset = $(this).offset().top;
			    	$('html,body').animate({scrollTop: targetOffset}, 1000);
					returnvalue = false;
					return false;
				}
  }
  else
  {
    return true;
  }
}
}

Thanks in advance and have a great new year everybody!
Joseph

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.