Jump to content

Validating Fields and Warning Boxes


tyv

Recommended Posts

Hi, I have a form with multiple fields that are confirmed before the form is submitted (ex. email field needs to be completed before the form can be submitted). Once the required fields are completed, I want a final warning box to appear asking the user "are you sure you want to make these changes?" I'm having trouble getting the function to work correctly. Here's the problem I've encountered:First Script: this one lets me enter an email into an empty email field and shows the confirmation box but the information in the form is updated regardless of whether I click OK or Cancel. function page_check(){if (check(document.form.txt_EMail.value,true)) { if ((document.form.txt_EMail.value!="") && (check(document.form.txt_EMail.value,false))) document.form.submit(); else { if (document.form.txt_EMail.value=='') alert('Please enter an email.'); return; } if(confirm("Are you sure you want to upload this file?")) { document.form.page_check() return false; } }}Second Script: When I create a separate function with "are you sure you..." warning similar to this:function page_check(){ if(confirm("Are you sure you want to upload this file?")) { document.form.page_check() return false; } }else return true;} it does not prompt me to complete the email field. It goes straight to the warning, "are you sure..." However, if I click Cancel, then it does not update, and if I click OK it updates. Any suggestions on how to fix this script so that I can keep the enter email prompt AND the warning prompt? Any help is very much appreciated. Thanks in advance :).

Link to comment
Share on other sites

It's much easier to debug when the code is formatted.

function page_check(){  if (check(document.form.txt_EMail.value,true))  {	if ((document.form.txt_EMail.value!="") && (check(document.form.txt_EMail.value,false)))	  document.form.submit();	else	{	  if (document.form.txt_EMail.value=='')		alert('Please enter an email.');	  return;	}	if(confirm("Are you sure you want to upload this file?"))	{	  document.form.page_check()	  return false;	}  }}

The logic is all screwed up. That function doesn't do anything if the first condition fails, whatever the check function does. If the first condition fails then the function doesn't return anything. If the first condition passes then it evaluates the exact same condition with another value, which obviously is always going to fail. It's like doing this:

if (A == true){  if (A == false)	alert("this will never happen");}

Unless your "check" function is doing something else, but you didn't post the code for that so I have to guess. Regardless though, your function never returns true, and it only returns false if they entered an email value and then clicked OK on the confirm box, it doesn't return true or false under any other circumstances. There is a statement to return an undefined value, but that statement will never get executed because of the logic in the IF statements. Also, why the heck are you calling the same function recursively if they click OK on the confirm box?The second function you posted isn't even syntactically correct:

function page_check(){  if(confirm("Are you sure you want to upload this file?"))  {	document.form.page_check()	return false;  }}elsereturn true;}

Link to comment
Share on other sites

Firstly I'll solve all your syntax errors:

function page_check() {  if (check(document.form.txt_EMail.value,true)) {	if ((document.form.txt_EMail.value!="") && (check(document.form.txt_EMail.value,false))) {	  document.form.submit();	} else {	  if (document.form.txt_EMail.value=='') alert('Please enter an email.');	  return;	}	if(confirm("Are you sure you want to upload this file?")) {	  document.form.page_check()	  return false;	}  }}function page_check() {  if(confirm("Are you sure you want to upload this file?")) {	document.form.page_check()	return false;  } else {	return true;  }}

Second of all: You've given two functions the same name.Third problem: function check() does not exist.

Link to comment
Share on other sites

Hi, Would you be able to suggest how to apply multiple if statements to a function instead of creating multiple functions? I want my submit button to check for valid fields and then warn the user before uploading anything. The function is page_check_pwd. Thanks in advance for any help. :)tyv

Firstly I'll solve all your syntax errors:
function page_check() {  if (check(document.form.txt_EMail.value,true)) {	if ((document.form.txt_EMail.value!="") && (check(document.form.txt_EMail.value,false))) {	  document.form.submit();	} else {	  if (document.form.txt_EMail.value=='') alert('Please enter an email.');	  return;	}	if(confirm("Are you sure you want to upload this file?")) {	  document.form.page_check()	  return false;	}  }}function page_check() {  if(confirm("Are you sure you want to upload this file?")) {	document.form.page_check()	return false;  } else {	return true;  }}

Second of all: You've given two functions the same name.Third problem: function check() does not exist.

Link to comment
Share on other sites

Code inside functions isn't any different then code anywhere else, you can use the same control structures that you can outside of a function. Keep track of a value to indicate if the form is valid, start it at true, and if you find anything that is invalid set the variable to false. At the end of the function return the variable.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...