Skip to main content
Inspiring
November 3, 2008
Question

cfinput validation question

  • November 3, 2008
  • 4 replies
  • 1315 views
I am using the following to validate my data :

<cfinput type="text" name="unitValue#gfmPartNumberID#" value="#qryView.unitValue#" required="Yes" validate="float" message="Unit Value must be entered and must be numeric.">
<input type="hidden" name="gfmPartNumberID" value="#qryView.gfmPartNumberID#">

If I use validate=integer, it will not take decimals. If I use validate=float, it will take decimal input such as 19.99 but when I display, the outptu is 19.00 only. Can cfinput validate decimal inputs ?

If not, can I use javascript to validate if my field name is unitValue#gfmPartNumberID# ? I am using this to distinguish between different records.

Thanks
    This topic has been closed for replies.

    4 replies

    BKBK
    Community Expert
    Community Expert
    November 11, 2008
    I use set unitValue = '#Evaluate("form.unitValue#gfmPartNumberID#")#'

    There is no need for the complexity. This should do it:

    <cfset unitValueField = "unitValue" & gfmPartNumberID>
    <cfset unitValue = form[unitValueField]>


    BKBK
    Community Expert
    Community Expert
    November 4, 2008
    For example, if I enter 346.88 in the form, the table has it stored
    as 347, thus my output with dollarformat is $347.00. The table is
    in sql server and the field is defined as datatype decimal, length 9.


    Test with:

    <cfparam name="form.unitValue" default="346.88">
    <cfoutput>#dollarFormat(form.unitValue)#</cfoutput>

    You should get $346.88, as expected. So, the rounding off is happening at the database server.

    You should use a second parameter('scale') for the decimal datatype, for example, decimal(9,2). If you omit the scale, and use, for example, decimal(9), SQL Server will use a default scale of 0 and the data will effectively be stored as an integer.



    trojnfnAuthor
    Inspiring
    November 11, 2008
    I changed the data type in the sql server to float, and now it stores the decimal and displays properly.

    However, by making this chagne, I solved one problem but seem to have created another. When I attempt to update, I use set unitValue = '#Evaluate("form.unitValue#gfmPartNumberID#")#'

    But it blows up and says something about cannot evaluate. Before I chagned the data type to float, it was set at int and the update worked but it did not save the decimals.

    Now I have the decimals but the update does not work because of float. What do I need to do to keep the decimals and to get the update to work ?
    Inspiring
    November 4, 2008
    quote:

    Originally posted by: trojnfn
    If not, can I use javascript to validate if my field name is unitValue#gfmPartNumberID# ? I am using this to distinguish between different records.

    Thanks

    You shouldn't have to.
    Inspiring
    November 4, 2008
    > I display, the outptu is
    > 19.00 only. Can cfinput validate decimal inputs ?

    *input* validation has no bearing whatsoever on what gets output once you
    submit the form. If the user types in 19.99, then 19.99 will be what is
    passed to the action page. If your output is outputting something else,
    then it's the output that is the problem.

    Post your code.

    --
    Adam
    trojnfnAuthor
    Inspiring
    November 4, 2008
    I just use #dollarformat(unitValue)# to display the output with the dollar sign and it just puts 00 after the decimal.

    When I insert into the table, I use this line :
    <cfset col4 = listgetat(form.unitValue, i)>
    which is inside a loop.

    When I check the table contents, it is stored rounded up. For example, if I enter 346.88 in the form, the table has it stored as 347, thus my output with dollarformat is $347.00. The table is in sql server and the field is defined as datatype decimal, length 9.

    So you are right, the output is displaying correctly. Now the next question is how do I insert into the table exaclty what is entered on the screen ? If 346.88 is entered, I want that stored and displayed as $346.88.

    Thanks