View Single Post
  #2 (permalink)  
Old 02-18-2008, 09:45 PM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,321
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Thumbs up ColdFusion Tips & Tricks

Allowing commas in a numeric CFINPUT text field


Normally, you can not enter numbers with a comma in a numeric CFINPUT field, the Javascript function generated by Cold Fusion will reject it.

Nice thing about Javascript, you can define a function more than once without it going kablooie all over your nice computer. The function defined last is the one used.

Here's an example. I copy-n-pasted the generated Javascript into the template and added a comma to the list of approved numeric characters.

Code:
<CFFORM name="MyForm">
  <cfinput type="Text" name="i_value" validate="float" message="Bad value! Bad!">
</cfform>
 
<script language="Javascript">
// Override the numeric validation generated by Cold Fusion
function _CF_checknumber(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false
 
    if (object_value.length == 0)
        return true;
 
    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
   
 var start_format = " .+-0123456789";
    // Comma added to allowed characters
 var number_format = " .,0123456789";
 var check_char;
 var decimal = false;
 var trailing_blank = false;
 var digits = false;
 
    //The first character can be + - .  blank or a digit.
 check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
 if (check_char == 1)
     decimal = true;
 else if (check_char < 1)
  return false;
       
 //Remaining characters can be only . or a digit, but only one decimal.
 for (var i = 1; i < object_value.length; i++)
 {
  check_char = number_format.indexOf(object_value.charAt(i))
  if (check_char < 0)
   return false;
  else if (check_char == 1)
  {
   if (decimal)  // Second decimal.
    return false;
   else 
    decimal = true;
  }
  else if (check_char == 0)
  {
   if (decimal || digits) 
    trailing_blank = true;
        // ignore leading blanks
 
  }
         else if (trailing_blank)
   return false;
  else 
   digits = true;
 } 
    //All tests passed, so...
    return true
    }
</script>
__________________
Prasanna Vignesh
MCPD | Web Developer
Reply With Quote