PDA

View Full Version : tricky set of conditional statements, question


Burgerking63
06-10-2005, 09:30 PM
Alright, I've got a fairly tricky script that validates user input for a form and allows/disallows them to submit the form based on what they input or what the conditions are. I had it working fine before I needed to have the first radio button disable input into the filenumber text field.

Here's what the form does now:
Essentially, on submission, it checks to see if there is input in the filenumber field (if not, makes user submit something), then does the same for the submitted_by field. If both fields have some text, it prompts the user if they want to submit the form, or make more changes via a confirm. I have all this working.

Here's what I want it do now: Now, I want to have the filenumber field dynamically change depending on the select of the form type radio buttons. If "new" is selected, the filenumber field is disabled from allowing user input and the script should skip straight to the submitted_by validation function (validateName).

Otherwise if "change" is selected, it reads that the filenumber field is not disabled, then runs the filenumber field validation function (validateFilenum). After it validates the filenumber field, it goes to the submitted_by field validation function (validateName).

Now it runs this function and if the field is valid for submission, it prompts the user if they have everything entered properly or if they want to make changes via a "confirm" message.

The form SHOULD NOT SUBMIT untill after both fields have been validated to be acceptable for submission. What I am stuck on is how to structure the two functions with these IF/ELSE statements properly. Suggestions and corrections are greatly appreciated as I am lost in a sea of IF and ELSE...

Here is my code so far:

<html>
<head>

<script type="text/javascript">
function validateFileNum(f){
var filenumlen = f.filenumber.value;
var submitlen = f.Submitted_By.value;
if (f.filenumber.disabled = true)
{
validateName();
return false
}
else if ((Number(filenumlen) == 0)){
alert('Please enter the FILE NUMBER of the person this information pertains to and submit the form again');
f.filenumber.value='';
f.filenumber.focus();
return false
validateName();}
else {
return false;
validateName();
}
}
</script>

<script type="text/javascript">

function validateName(){
var submitlen = Edf.Submitted_By.value;
if ((Number(submitlen) == 0)){
alert('Please enter the NAME of the person submitting this form then submit the form again');
f.Submitted_By.value='';
f.Submitted_By.focus();
return false}
else if ((Number(submitlen) != 0)){
if (confirm('Have you filled out all the applicable fields properly? If so, click "Ok" to submit, or "Cancel" to make changes')){
f.submit();}
else {
return false}
}
}
</script>

</head>
<BODY BACKGROUND="http://www.winroc.com/index-images/no-black-bkgrnd.jpg" LINK="#0000FF">
<!-- Start of Form -->
<form ID="Empdataform" NAME="Edf" method="POST" enctype="application/x-www-form-urlencoded"
action="http://www.winroc.com/scripts/formmail.pl" onsubmit="return validateFilenum(this)">

<input type="hidden" name="recipient" value="">

<FONT FACE="Arial">Form Type: NEW
<INPUT TYPE="radio" VALUE="new" NAME="Formtype" onClick="Edf.filenumber.disabled=true">


CHANGE
<INPUT TYPE="radio" VALUE="change" ID="formtype" NAME="formtype" onClick="Edf.filenumber.disabled=false"></FONT>


<br><FONT FACE="Arial">File#:
<INPUT NAME="filenumber" ID="File_Number" TYPE="text" SIZE="15" maxlength="10"></FONT>

<br><br>


<b><u>Submitted By:</u>&nbsp;&nbsp;&nbsp;</b></u>
<input type="text" ID="Submitted" name="Submitted_By" value="" cols="40" size="40">
&nbsp;<b><i>Required Field</i></b>


<br>
<input type="submit" name="submit" value="SUBMIT">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


<input type="reset" name="reset" value="RESET" ONCLICK="return confirm('Are you sure you want to clear all the fields?')">

</form>
</body>
</html>

Please, please, please help me understand this conditional mess

Burgerking63
06-10-2005, 10:47 PM
Nevermind, I figured it out after some testing. Make sure if you do this to remember to re-enable your text field again with another event handler (i used the other options) AND also re-enable it onReset. I found if you dont have it enabled onReset, that box will still be disabled even if you have a reset on refresh script

here is the new script code:

<script type="text/javascript">
function validate(f){
var filenumlen = f.filenumber.value;
var submitlen = f.Submitted_By.value;
if (f.filenumber.disabled == false)
{
if ((Number(filenumlen) == 0))
{
alert('Please enter the FILE NUMBER of the person this information pertains to and submit the form again');
f.filenumber.value='';
f.filenumber.focus();
return false
}

if ((Number(submitlen) == 0))
{
alert('Please enter the NAME of the person submitting this form then submit the form again');
f.Submitted_By.value='';
f.Submitted_By.focus();
return false
}
if ((Number(filenumlen) != 0)&&(Number(submitlen) != 0))
{
if ((confirm('Have you filled out all the applicable fields properly? If so, click "Ok" to submit, or "Cancel" to make changes')))
{
f.submit();
}
else
{
return false
}
}
}
else
{
if ((Number(submitlen) == 0))
{
f.filenumber.value='n/a';
alert('Please enter the NAME of the person submitting this form then submit the form again');
f.Submitted_By.value='';
f.Submitted_By.focus();
return false }
if ((f.filenumber.disabled=true)&&(Number(submitlen) != 0))
{ if ((confirm('Have you filled out all the applicable fields properly? If so, click "Ok" to submit, or "Cancel" to make changes'))) {
f.submit(); }
else {
return false }
}
}
}
</script>