A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [RESOLVED] Why Does The Form SKIP the last Else If statment?

  1. #1
    Member
    Join Date
    Dec 2007
    Posts
    71

    resolved [RESOLVED] Why Does The Form SKIP the last Else If statment?

    Hi, Im learning javascript but I know enough to successfully modify some scripts. Well I need some help with this one. I have distilled it down and have configured it the way I want. The only thing is that I can not understand why the form skips the last Else If statement. Can someone please help me pin point this? I have used an alert and it works successfully on the first 2 statements when the condition is met, but it just pushes the form through the last statement even when the conditions are invalid. Here's the code:

    Thanks in advance for taking a look.





    Code:
    <script language="JavaScript">
    
    var theForm = null;
    var ERROR = false;
    
    function validateForm ()
       	{
    	try
    		{
    		theForm = document.forms[0];
    		theForm.elements.errorMsg.value = "";
    
    		var errorMessage = "";
    		var success = true;
    
    		var fullName = theForm.elements.fullName.value;
    		var email = theForm.elements.email.value;
    		var station = theForm.elements.station.value;
    
    		theForm.elements.action.value        = "send";
    		theForm.elements.postingPage.value   = document.location.href;
    		theForm.elements.postingHost.value   = document.location.href.substr(0, document.location.href.lastIndexOf("/")+1);
    
    
    		if (fullName == "" || fullName.indexOf(" ") == -1)
    			{
    			errorMessage = "Please enter both your first and last name.";
    			success = ERROR;
    			}
    
    		else if (email != "" && (email.indexOf("@") == -1 || email.indexOf(".") == -1 || email.indexOf(" ") != -1 || email.length < 6))
    			{
    			errorMessage = "Please check your e-mail address.";
    			success = ERROR;
    			}
    			/* THIS IS WHERE I PERCEIVE THE PROBLEM BEGINS */
    			else if (station == "")
    			{
    			errorMessage = "Please enter your station or phone.";
    			success = ERROR;
    			}
    			
    		var jsLine = theForm.elements.errorMsg;
    		jsLine.className=(jsLine.className=='hidden')?'unhidden':'hidden';	
    		jsLine.value = errorMessage;
    
    		theForm.elements.geoLocation.value = "@IP | " + geoip_country_code() + ": " + geoip_country_name()
    			+ " | " + geoip_city() + ", " + geoip_region()
    			+ " | lat=" + geoip_latitude() + ", lon=" + geoip_longitude();
    
    		var firstName = fullName.split(" ")[0];
    		var lastName = fullName.replace(firstName+" ", "");
    		theForm.elements.firstName.value = firstName;
    		theForm.elements.lastName.value = lastName;
    
    		return (success) ? true : false;
    		}
    	catch (_e)
    		{                                                                                                            			
    		var msg= "There was an internal error processing the form!  \nPlease call XXXX XXXX at 1-800-67-XXXX.  \n\n"+_e.description;
    		theForm.elements.errorMsg.value = msg;
    		theForm.elements.errorMsg.scrollIntoView();
    		confirmOkay(msg, null, confirmation.ERRORICON);
    		return false;
    		}
        }
    
    /*
    function redirectPage ()
    	{
    	switch (document.all.to.selectedIndex)
    		{
    		case 0:
    			document.location = "xxxx.asp";
    			break;
    		case 1:
    			document.location = "xxxx.asp";
    			break;
    		case 2:
    			document.location = "xxxx.asp";
    			break;
    		}
    	}
    */
    	
    	function SelectAll(id)
    {
        document.getElementById(id).focus();
        document.getElementById(id).select();
    }
    
    </script>
    
    
    
    </head>
    <body onload="window.resizeTo(730,835);window.focus();">
    
    <noscript>
    	<span style="font:12px Verdana; color:red; font-weight:bold;">JavaScript must be enabled to use our contact form.</span>
    </noscript>
    
    
    <%  
    	var requestType = "xxx Popup";
    %>
    
    
    	
    	
      <div id="contact_form_wrap">
        <div id="ballText">&nbsp;</div>
    	
    <form id="theForm" name="theForm" method="post" action="processPopUpForm.asp" onsubmit="return validateForm()">
    
    
    <table class="cont" border="0" cellspacing="0" cellpadding="5">
    	<tr>
    		<td>
    			<input type="text" id="fullName" name="fullName" onClick="SelectAll('fullName');" class="textfield" size="25" maxlength="50" value="Full_Name" style="width:250px;"></input>
    		</td>
    		<td width="50px">&nbsp;</td>
    	</tr>
    		<tr>
    		<td>
    			<input type="text" id="emltxt" name="email" onClick="SelectAll('emltxt');" class="textfield" size="25" maxlength="60" value="Enter Your Email" style="width:250px;"></input>
    		</td>
    		<td width="50px">&nbsp;</td>
    	</tr>
    	
    	<tr>
    		<td>
    			<input type="text" id="stationName" name="station" onClick="SelectAll('stationName');" class="textfield" size="25" maxlength="50" value="What's Your Station?" style="width:250px;"></input>
    		</td>
    		<td width="50px">&nbsp;</td>
    	</tr>
    	
    	<tr>
    		<td colspan="4" valign="top" height="26">
    		<input type="text" name="errorMsg" class="hidden" style="border:0px; clip:auto; overflow:hidden; width:100%; text-align:center; color:#ffff33; font-weight:bold;" readonly></input>
    			<div align="left">
    			<input type="submit" class="button" value="" name="button1"></input> &emsp;<input type="reset" class="reset" name="reset" value="" onclick="window.location.reload(true);" />
    			</div>
    		</td>
    	</tr>
    </table>
    
    <input type="hidden" name="requestType"   value="<%=requestType%>"></input>
    <input type="hidden" name="contactMethod" value="e-mail"></input>
    <input type="hidden" name="action"        value=""></input>
    <input type="hidden" name="postingPage"   value=""></input>
    <input type="hidden" name="postingHost"   value=""></input>
    <input type="hidden" name="station"      value=""></input>
    <input type="hidden" name="geoLocation"   value=""></input>
    <input type="hidden" name="firstName"   value=""></input>
    <input type="hidden" name="lastName"   value=""></input>
    
    </form>
    <script language="JavaScript">
    
    <% if (requestType == "MM Sales Inquiry") { %>
    		//setTimeout('document.getElementById("marketSelect").focus();', 500);
    <% } else { %>
    		//setTimeout('document.getElementById("button1").focus();', 500);
    <% } %>
    </script>

  2. #2
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    What I suggest you do is trace station to see what exactly it's producing. Trace it with some surrounding values like trace("-"+station+"-"); This will help you see what it's getting to begin with.
    .

  3. #3
    Member
    Join Date
    Dec 2007
    Posts
    71

    Trace?

    Thanks for your reply. How would I do such a trace?

  4. #4
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    trace(station);
    if (fullName == "" || fullName.indexOf(" ") == -1)
    .

  5. #5
    Member
    Join Date
    Dec 2007
    Posts
    71

    tried the trace... unsuccessful

    I've tried the trace statement and the form is still getting pushed through.

    I replaced the if else statement with the trace you gave me:

    Code:
    			trace(station);
    			if (fullName == "" || fullName.indexOf(" ") == -1)
    			{
    			alert("testing testing");
    			}

    am I not attempting this correctly?

  6. #6
    Member
    Join Date
    Dec 2007
    Posts
    71

    Found the issue!

    Hi... I finally found and fixed the bug.

    It seems that this "hidden" input tag at the end of the form code was the culprit:

    Code:
    <input type="hidden" name="station" value=""></input>
    Not sure why this had such a adverse effect on the script, but removing or commenting the snippet out now allows the js if else logic to work as intended.

    Thank you so much for taking the time to advise.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center