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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation function and return priority

Status
Not open for further replies.

travisbrown

Technical User
Dec 31, 2001
1,016
I'm trying to write one function that will groups together a couple form validation functions. The config below is the only way I can get it to run properly, but I don't understand why I put return on the second function in CheckLineSelectDate(), but not on the first. If I put return on both, it submits the form (this is onsubmit). I'm guessing that if the first function returns true, it allows the submit action before firing the second function? I'm just trying to understand what the priority is.

Cheers.

Code:
function CheckDateOrder(first_date,second_date) {
	var fd = document.getElementById(first_date).value;
	var sd = document.getElementById(second_date).value;
	if ( Date.parse(fd) > Date.parse(sd) ) {
	alert("Invalid Date Range.\nStart Date cannot be after End Date!");
	return false;
}
}

function CheckSelect(id,msg) {
	if ( document.getElementById(id).selectedIndex == 0 ) {
		alert(msg);
		return false;
	}
}

function CheckLineSelectDate() {
	CheckSelect('group_id','Please select a group.');
	return CheckDateOrder('list_date_start','list_date_end');
}
 
Hi

Code:
function CheckLineSelectDate() {
  return CheckSelect('group_id','Please select a group.') [red]&&[/red] CheckDateOrder('list_date_start','list_date_end');
}

Feherke.
 
but I don't understand why I put return on the second function in CheckLineSelectDate(), but not on the first

function CheckLineSelectDate() {
CheckSelect('group_id','Please select a group.');
return CheckDateOrder('list_date_start','list_date_end');
}

You don't put the return on the first function because anytime you hit a return in a function, the function stops executing any statements below the return. In your case, you would never execute this statement:

Code:
return CheckDateOrder('list_date_start','list_date_end');

On your CheckSelect function though, you do need to use the value assigned to it. So it will look something like this.

Code:
function CheckLineSelectDate() {
   if (CheckSelect('group_id','Please select a group.')) {
      return CheckDateOrder('list_date_start','list_date_end');
   }
   else {
      return false;
   } 
}

[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top