IT Community - Software Programming, Web Development and Technical Support

Allowing commas in a numeric CFINPUT text field

This is a discussion on Allowing commas in a numeric CFINPUT text field within the ColdFusion Programming forums, part of the Web Development category; Normally, you can not enter numbers with a comma in a numeric CFINPUT field, the Javascript function generated by Cold ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > ColdFusion Programming

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 02-08-2009, 07:53 PM
hkp819 hkp819 is offline
D-Web Trainee
 
Join Date: Dec 2008
Posts: 41
hkp819 is on a distinguished road
Default 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. 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>
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you limit the length (in characters) of text entered in a textarea form field GDevakii HTML, CSS and Javascript Coding Techniques 2 03-24-2008 07:41 PM
Prefill a text form field with the current date. itbarota HTML, CSS and Javascript Coding Techniques 1 03-18-2008 08:09 PM
How do I check that text entered in a text form field is an integer? itbarota HTML, CSS and Javascript Coding Techniques 2 10-25-2007 11:25 PM
How can I ensure that one radio button is checked and that the relevant text field is itbarota HTML, CSS and Javascript Coding Techniques 1 10-17-2007 10:21 PM
numeric variable can hold vigneshgets C and C++ Programming 0 05-18-2007 03:24 AM


All times are GMT -7. The time now is 05:27 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0