Issue with Form validation

Discussion in 'JavaScript' started by cfnewb, Jan 17, 2008.

  1. #1
    Hi fellow programmers, I kinda run into a bit of a jam for 1-1/2 days now with this form validation. Something is wrong with my if statement in the Function validateForm(form). If I take out the if statement, my alert is displayed on the screen. If I add the if statement, my alert does not display on the screen. Anyone see anything wrong with the code in the function as well as in the input/form and how the TSize name is reference in the function via the if statement? I think I have narrowed it down to the if statement but I have no clue if I am referencing the value of TSize correctly in the if statement. Thanks in advance for any helps.

    
    <!---this form sets T value--->
    
    <cfif not IsDefined("TSize")><cfset TSize = ""></cfif>
    
    <script language="JavaScript"><!--
    		
    function validateForm(form){
    	if (form.document.breast6.TSize.value =="") {
       	alert("Please enter a size.");
       	return false;
    	} else {
    		return true;
      }
    }
    
    // --></script>
    
    <cfset diseasesite = "#site#">
    <cfinclude template=stdheader.cfm>
    
    <tr>
    	<td colspan=2 bgcolor=ffFFff height=300 valign=top>&nbsp;</td>
    	<td colspan=2 bgcolor=ffFFff>
    	<cfoutput>
    	<font size=2 face="Arial, Helvetica">
    	<form name="breast6" method="post" action="breast6.cfm" onSubmit="return validateForm(this)">
    	<b>Tumor Size:  </b><input type="text" name="TSize" size="10" value="">
    	<P>
    	(Use 00.0 if no evidence of primary tumor or 99.9 if size cannot be assessed)
    	<input type="hidden" name="site" value="#site#">
    	<input type="hidden" name="type" value="#type#">
    	<input type="hidden" name="m" value="#m#">
    	<input type="hidden" name="n" value="#n#">
    	<input type="hidden" name="t" value="Unknown">
    	<input type="hidden" name="TSize" value="#TSize#">
    	<table>
    	<tr><td></td></tr>
    	<tr>
    	<td align=left></td>
    	<td>
    	<input type="submit" value="Continue ...">
    	</td>
    	</tr>
    	</table>
    	</form>
    	</font>
    	</cfoutput>
    	</td>
    	<td bgcolor=ffFFff>&nbsp;</td>
    </tr>
    
    <cfinclude template=stdfooter.cfm>
    
    Code (markup):
    If I change the above function validateForm(form) to

    
    function validateForm(form){
       	alert("Please enter a size.");
       	return false;
    }
    
    Code (markup):
    The alert appears on the screen.
     
    cfnewb, Jan 17, 2008 IP
  2. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #2

    Give that form an ID.

    And use this:

    
    document.getElementById('id of element').value
    
    Code (markup):
    Then eval on that. (IE: == "")
     
    LittleJonSupportSite, Jan 17, 2008 IP
  3. cfnewb

    cfnewb Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sorry, I don't quite understand the use of id. I have looked at www.w3schools.com/html but it is still not clear how I would use id in this cenario.

    Edit: I am still a little haze on why I need the id. But I got it to work. Thanks to LittleJon helpful advice!
    Here is what I changed in the code.

    function validateForm(form){
    var TSvalue=document.getElementById("yyy").value;
    if (TSvalue =="") {
    alert("Please enter a size.");
    return false;
    } else {
    return true;
    }
    }



    <b>Tumor Size: </b><input type="text" id="yyy" name="TSize" size="10" value="">
    <P>
     
    cfnewb, Jan 17, 2008 IP
  4. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This way, is "standards":
    
    <script type="text/javascript">
    		
    function validateForm(nForm){
    
    	if (nForm['TSize'].value == "") 
    		{
       		 alert("Please enter a size.");
    		 return false;
    		}
    	return true;  
    }
    
    </script>
    
    Code (markup):
     
    Mike H., Jan 17, 2008 IP
  5. cfnewb

    cfnewb Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks Mike, will try that too. Everyone been very helpful!
     
    cfnewb, Jan 18, 2008 IP