Jump to content

Checking multiple conditions


mrsquash

Recommended Posts

Hello all,

 

I am having a problem with my form validation.

 

Basically, I have a search form that contains six(6) aeras to search by. (Request ID, Date Entered, Last Name, First Name, Status, and Inquiry Type) At a minimum I want to require the user to select an Inquiry Type. To do this I am including a form validation function in javascript.  Below is what I have.

 

function submitForm() {

// Requires the user to select the Inquiry Type at a minimum
if (document.submitForm.request_id.value != "" || 
document.submitForm.date_entered.value != "" ||
document.submitForm.last_name.value != "" ||
document.submitForm.first_name.value != "" ||
document.submitForm.status.options.value != "" &&
document.submitForm.inquiry_code.options.value == "") {
alert("You must select an Inquiry Type in order to view any records.");
submitForm.inquiry_code.focus();
return;
}

document.submitForm.submit();
}

 

This "should" check to see if any values were entered into any of the search criteria. IF anything was entered, but the Inquiry Type was not selected it should return the error.

 

But...it's not working.

 

Any ideas?

Link to comment
Share on other sites

wow ok there alot of problems here.

 

first you need to use the getElementById method to aquire something instead of just the id value.

 

also that is a hugh conditonal which would take along time to run through for each field.

 

chanceis are you are caught in an infinte loop or timing out.

 

here try this i use it with some of my forms

 

/*
	Required field(s) validation- By NavSurf
	Visit NavSurf.com at http://navsurf.com
	Visit http://www.dynamicdrive.com for this script
	*/

	function formCheck(formobj){
		//1) Enter name of mandatory fields
		var fieldRequired = Array("firstname", "lastname", "company", "address", "city", "StateorProvince", "postalcode", "country", "phone");
		//2) Enter field description to appear in the dialog box
		var fieldDescription = Array("First Name", "Last Name", "Company", "Address", "City", "State/Providence", "Postal Code", "Country", "Phone");
		//3) Enter dialog message
		var alertMsg = "Please complete the following fields:\n";

		var l_Msg = alertMsg.length;

		for (var i = 0; i < fieldRequired.length; i++){
			var obj = formobj.elements[fieldRequired[i]];
			if (obj){
				switch(obj.type){
					case "select-one":
					if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
						alertMsg += " - " + fieldDescription[i] + "\n";
					}
					break;
					case "select-multiple":
					if (obj.selectedIndex == -1){
						alertMsg += " - " + fieldDescription[i] + "\n";
					}
					break;
					case "text":
					case "textarea":
					if (obj.value == "" || obj.value == null){
						alertMsg += " - " + fieldDescription[i] + "\n";
					}
					break;
					default:
					if (obj.value == "" || obj.value == null){
						alertMsg += " - " + fieldDescription[i] + "\n";
					}
				}
			}
		}

		if (alertMsg.length == l_Msg){
			return true;
		}else{
			alert(alertMsg);
			return false;
		}
	}

 

all you need to do is alter the required fields array and the description array to suit your form.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.