Quantcast
Channel: Adobe Community : All Content - ColdFusion
Viewing all articles
Browse latest Browse all 5979

validate decimal

$
0
0

Hi. I have a form for a user to type in a number into a text box. I want to allow them to enter whole numbers and numbers with 1 decimal place. If they enter a number with more than 1 decimal place, I want the form to stop from submitting, or pop up a window that says they have to enter a number with only 1 decimal place. Can this be done with Cold Fusion's validation code in the form, or does it have to be written in Javascript in order for this to work? I do have some javascript code below that I used on another page that allows 2 decimal places, but I cannot get this javascript to work. It pops up a window that says I need to enter 2 decimal places, but the form still gets submitted. How do I stop the form from being submitted until it is entered correctly? I don't understand Javascript very well. This is why I was wondering if there was an easier way with Cold Fusion's validation code instead? Here's the code that works, but doesn't stop the form from submitting:

 

 

<script type="text/javascript">

function newItem()

 

{

 

//This code below is so that the hours fields are entered correctly with whole numbers and decimal points such as 1, 1.0, 1.02, etc.

if(!validateHours()){

                                //do nothing

                }

                else

                {

                document.AddECNumber.action ="add_newitem_existing_eco.cfm";

                document.AddECNumber.submit();

                }

                 

                }

 

                var validateHours = function () {

 

                                var fields = document.getElementsByClassName("field");

 

                                var success = true;

                                for(var t = 0; t< fields.length; t++){

                                                success = validateInput(fields[t].value, fields[t].getAttribute("label"));

 

                                                if(!success){

                                                                break;

                                                }

                                }

 

                                return success;

                }

                var validateInput = function (val, name) {

  var regex = /^[0-9]\d*(((,\d{3}){1})?(\.\d{0,2})?)$/;

                                var success = true;

 

                                if(val === "") {

                                                return success;

                                }

                               

                                if(val.indexOf(".") > 0 && val.length - (val.indexOf(".") + 1) > 2){

                                alert(name + " cannot have more than 2 decimal places.");

                                success = false;

                                }

                                if (success && !regex.test(val.replace("$", ""))){

                                alert("Please enter a valid value for " + name);

                                success = false;

                                }

 

                                return success;

                }

 

                //  End -->

</script>

 

 

<cfif NOT isDefined("form.submit")>

 

<cfif isDefined('form.X')>

<cfset page.select_X = form.X>

</cfif>

 

</cfif>

 

<div align="center">

<table width="650" cellpadding="4" cellspacing="0" border="0">

 

<cfoutput>

  <form name="DropDown" method="post">

 

<tr>

<td class="edit" align="right">Xmm:</td>

<td>

<cfif Not isDefined('form.select_X')>

<input type="text" name="X" class="field" attribute label ="Xmm" required="required" size="6">

</cfif>

</td>

</tr>

 

<tr>

<td> </td>

</tr>

 

<tr>

<td> </td>

<td>

<input type="submit" onchange="this.form.submit()" onclick="newItem()" value="Update"> 

<input type="reset" value="Reset">

 

</form>

</td>

</tr>

 

</table>

 

<p>

 

<cfif isDefined('page.select_X')>

 

<table cellpadding="4">

 

<tr>

<td>Xmm:</td><td>#X#</td>

</tr>

 

</table>

 

</cfif>

 

</cfoutput>

</div>

 

Thanks.

Andy


Viewing all articles
Browse latest Browse all 5979

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>