Copy link to clipboard
Copied
Hi. I have a field in my SQL database that is an integer field. When I try to use this field in my form, it only allows whole numbers to be entered. How can I allow numbers with decimal points to be entered? The field name is called Eng_Hrs. Here's what I have on the Form:
<tr>
<td class="edit" align="left">Engineering Hours: <input type="text" name="Eng_Hrs" size="12" validate="integer" <!--- required="no" ---> message="You must enter a valid number in the Engineering Hours field"></td>
</tr>
I realize I could make this a text field and it would allow decimal points, but then how could I validate this field to make sure that only whole numbers or numbers with decimal points are entered?
Thanks.
Andy
Copy link to clipboard
Copied
You will need to validate the field relative to the SQL field.
You can set the SQL field as decimal or numeric (decimal and numeric) which will allow only numbers and then validate against that. Then set the validate to numeric.
I would recommend not using cfinput though. If you are not using existing code, just stick with html and javascript for validation.
Copy link to clipboard
Copied
I set the SQL field to be decimal or numeric, but when I try to enter a decimal number like 2.5 directly into the database, it still rounds it up to 3. I think I need to get that working first before I do any validation on the Cold Fusion code. Do you know why it's rounding up to a whole number still? Should't it allow it to be 2.5 if I have it set to be Decimal or Numeric?
Copy link to clipboard
Copied
It depends how you set the data type.
In SQL Server, as mentioned in the link in my last post, decimal and numeric take arguments.
So if you wanted a number up to to 10 numbers long and have 2 decimal points then you would set it to numeric(10,2)
Copy link to clipboard
Copied
Haxtbh,
Thanks! I wasn't sure what those numbers meant. Now it makes sense. What's the difference between decimal and numeric? Does either one work for what I want?
Now that I'm able to get numbers with 2 decimals entered into the database, how can I validate this in the code so that only numbers with 2 decimal places can be entered? I just don't want someone to accidentally type text into this field. I would like an error to pop up if they do enter some text in it.
Andy
Copy link to clipboard
Copied
Decimal and numeric are the same.
To validate you can use the validate type numeric.
<input type="text" name="Eng_Hrs" size="12" validate="numeric" <!--- required="no" ---> message="You must enter a valid number in the Engineering Hours field"></td>
I see you use an input though, not cfinput, are you already using a third party plugin? If so, then you will need to check their documentation.