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 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) 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> </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> </td> </tr> <cfinclude template=stdfooter.cfm> Code (markup):
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
Thanks Jamie, the alert really helps (at least it helps get me going again ) kudos! What is the best way to check and make sure the user enter a number and not a char?
For javascript you could use the isNaN function http://www.w3schools.com/jsref/jsref_isNaN.asp Another option is to use a CFFORM/CFINPUT and use validate="float" or validate="integer". http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-p65.htm But don't rely on just javascript. Re-check the value is numeric when the form is submitted.