dynamicreport.com News Member Login Knowledge Base Site Map
Support Support Ticket Contact Us
Products Drag & Drop Objects Drag & Drop Calendar UNIGEN Web Reporting Code View
Testimonials Downloads
Order

Knowledge Base Print printer-friendly page.

Login

UNIGEN

ddobj

News



Article #1010: Auto Correct Numeric Data Entry for Data Validation

 

The following javascript code will allow an application to filter bad input inside of a text box that needs to process valid numeric data according to a format. Only numbers, an optional negative sign at the start of the input, and a single decimal point are considered valid elements of the input. All other characters are stripped out as part of the data validation. This auto input correction is a useful way to avoid database errors during a write operation, which can occur when writing alphanumeric input into a numeric table field or when writing a real number into a strict integer table field.

The function call may be placed in any triggering event such as the input text box onBlur event. The first parameter to the filter is the input box string. The second parameter will specify the number of decimal places to format and round the number. A value of 0 will specify formatting and rounding to whole integers. A value of 2 will format and round the input to the nearest hundredth decimal place. The third parameter will indicate whether or not negative numbers are acceptable. The decimal point character should be replaced by a comma in other localities such as the EU.

Note that the javascript rounding function result supported by different browsers varies in their implementation of the storage of numbers. IE stores floating point values differently from Mozilla-based browsers and therefore certain numbers will be rounded either up or down with inconsistencies across the various browsers. Rounding is often a result of floating point storage where the threshold for error will vary amongst different browsers, since for example the precision of repeating decimal numbers will be truncated or rounded. Number.toFixed(float places) will accomplish inconsistent rounding of a number depending on the browser used, and it is not supported by legacy browsers such as Netscape Communicator. Number.toFixed() can be replaced by a custom number rounding function. It is important to realize that normally when performing calculations, and depending on the application, only the output is formatted whereas precision values are stored internally for calculations. The final values are then rounded and formatted for visibility purposes as they are output so that precision is preserved throughout calculations. At other times it may be necessary to round amounts to a certain number of decimal places consistently before calculations are performed.

function input_filterAmt (str, dec, bNeg)
{ // auto-correct input - force numeric data based on params.
 var cDec = '.'; // decimal point symbol
 var bDec = false; var val = "";
 var strf = ""; var neg = ""; var i = 0;

 if (str == "") return;
 parseFloat ("0").toFixed (dec);
 if (bNeg && str.charAt (i) == '-') { neg = '-'; i++; }

 for (i; i < str.length; i++)
 {
  val = str.charAt (i);
  if (val == cDec)
  {
   if (!bDec) { strf += val; bDec = true; }
  }
  else if (val >= '0' && val <= '9')
   strf += val;
 }
 strf = (strf == "" ? 0 : neg + strf);
 return parseFloat (strf).toFixed (dec);
}

Code View - dynamicreport.com

The following HTML example applies the auto data entry correction when the focus from a text input is lost or the 'Update Values' button is pressed.

<form id = "frm_input" name = "frm_input" action = "" method = "post">
  <input id = "upd1" name = "upd1" type = "text" value = "0.00"
  style
= "text-align: right;" size = "5"
  onBlur
= "javascript: this.value = input_filterAmt (this.value, 2, 1);">
  2 decimal places (+/-)<br>
  <input type = "button" value = "Update Values"
  onClick
= "javascript: frm_input.upd1.value = input_filterAmt (frm_input.upd1.value, 2, 1);">
 </form>

Code View - dynamicreport.com

2 decimal places (+/-)

0 decimal places (+)

 

 


Back to article listing

 



 

Copyright © 2007-2008 Interaxis. All rights reserved.
Contact Webmaster