Jump to content

Help with if() with two conditions on form validation


Skipjackrick

Recommended Posts

I put a form validation code in the head of my documents but my select options have the value=null for the first value and some have value=0....Therefore,

 

Is it possible to set the "obj.value" to == 0  AND null ?

 

That way it will not let the user continue with a null value also.

 

function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("angler", "species_id", "mm", "dd");
// Enter field description to appear in the dialog box
var fieldDescription = Array("Angler", "Species Name", "Month", "Day");
// 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.value == 0){
				alertMsg += " - " + fieldDescription[i] + "\n";
			}
			break;
		case "select-multiple":
			if (obj.selectedIndex == -1){
				alertMsg += " - " + fieldDescription[i] + "\n";
			}
			break;
		case "text":
			if (obj.value == ""){
				alertMsg += " - " + fieldDescription[i] + "\n";
			}
			break;
		default:
		}
		if (obj.type == undefined){
			var blnchecked = false;
			for (var j = 0; j < obj.length; j++){
				if (obj[j].checked){
					blnchecked = true;
				}
			}
			if (!blnchecked){
				alertMsg += " - " + fieldDescription[i] + "\n";
			}
		}
	}
}

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.