How to manipulate a text input as a number for calculation?

Discussion in 'Programming' started by cfnewb, Dec 12, 2007.

  1. #1
    Hi,

    I am not sure how to take a text input and convert it into a number and do calculation on it. I have searched but I could not find any workable sample that meets my situation. Anyone have any samples or suggestions would be greatly appreciated.

    I have included a section of code. Looking at the code, you will probably see that I am fairly new at CF and web development :D

    The text input field is "size" and I have a validation check on size using the function validatSize. No matter what I enter for size, 0.9 or 1.3, the validation always returns the "Please enter a size (cm)" message. I can not get out of this mode to continue programming (basically I am stuck:confused:) Thanks.

    Here is the cold fusion method:
    
    <cfif not IsDefined("size")><cfset size = "0"></cfif>
    
    <script language="JavaScript"><!--
    
    function validateSize(form) {
      if ((document.breast5.size > 0)) {
        return true;
      } else {
        alert("Please enter a size (cm)");
        return false;
      }
    }
    
    // --></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">
    	<b>What is the size of tumor:</b>
    			<P>
    			<form name="breast5" method="post" action="breast5.cfm" onSubmit="return validateSize(this)">
    			<input type="text" name="size" value="#size#"> cm
    			<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">
    			<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):
     
    cfnewb, Dec 12, 2007 IP
  2. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    this is more of a javascript problem than a coldfusion problem..


    the problem is that document.breast5.size is an input object..
    document.breast5.size.value will return the value inside that input object


    use alerts() to do testing..

    i.e.
    function validateSize(form) {
      alert("document.breast5.size =" + document.breast5.size);
      alert("document.breast5.size.value =" + document.breast5.size.value);
      if ((document.breast5.size > 0)) {
        alert("returning true 1");
        return true;
      } else {
        if ((document.breast5.size.value > 0)) {
          alert("returning true 2");
          return true;
        }
        alert("Please enter a size (cm)");
        return false;
      }
    }
    Code (markup):
    size also sounds like it could be a keyword.. i'm not entirely sure if that could cause a problem
     
    Jamie18, Dec 12, 2007 IP
  3. cfnewb

    cfnewb Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Jamie, the alert really helps (at least it helps get me going again :D)

    kudos!

    What is the best way to check and make sure the user enter a number and not a char?
     
    cfnewb, Dec 12, 2007 IP
  4. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    cfStarlight, Dec 12, 2007 IP
  5. cfnewb

    cfnewb Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you so much, these are great references...
     
    cfnewb, Dec 13, 2007 IP