« 8.5 Certified | Main| Ad Placement Win »

Input Validation of Number Fields in Domino Web Applications

Tags: development JavaScript

In web applications Domino outputs decimal numbers stored in a Number field using the decimal separator which it deems right for the language set in the user's browser. If the numbers need to be edited in a web form, you need to make sure the users only enters valid numbers. For this, you might want to do an input validation in JavaScript to avoid unnecessary round trips to the server because of invalid numbers (being a good developer you will of course do a server side check as well, but that's not my point here).

Here's a little JavaScript function which uses regular expressions to check if a number is valid (thanks, Nicolai). It can be used for input validation and returns a boolean value. It should accept all number formats Domino accepts as well. Please let me know if you find it useful or if it needs to be improved.

The function uses two global variables containing the decimal and thousands separators.

/**
 * Check if the value of the input field is numeric and return false if not.
 * The global variable strDecimalSeparator contains the decimal separator 
 * Domino uses with the browser's locale.
 * It is used to convert and check the correct number format.
 * The global variable strThousandsSeparator contains the thousands separator 
 * Domino uses with the browser's locale.
 * @param {String} input value to check.
 * @version 1.1 build 2009-05-19
 * @author Nicolai Hess, SP Integration GmbH
 */
function isNumeric(input) {
  var strExpr = eval('/^-?\\d+$|^-?[1-9]+\\d?\\d?(\\'+ strThousandsSeparator +
    '\\d\\d\\d)*$|^-?[1-9]+\\d?\\d?(\\'+ strThousandsSeparator +'\\d\\d\\d)*\\' + 
    strDecimalSeparator + '?\\d+$|^-?\\d*\\' + strDecimalSeparator + '?\\d+$/');
	return strExpr.test(input);	
}
In oder to determine which separators Domino uses (and expects) in Number fields, I set these two global JavaScript variables in the HTML Head section of my form. Because Domino outputs numbers depending on the browser's language, we can simply extract the separators from numbers converted to text.
"<script type=\"text/javascript\">" + @NewLine +
"var strDecimalSeparator = \"" + @Middle(@Text(1,2); 1; 1) + "\";" + @NewLine +
"var strThousandsSeparator = \"" + @Middle(@Text(1000; ","); 1; 1) + "\";" + @NewLine +
"</script>" + @NewLine

Post A Comment

:-D:-o:-p:-x:-(:-):-\:angry::cool::cry::emb::grin::huh::laugh::lips::rolleyes:;-)

Search

Calendar

MiscLinks

We Use Ytria Lotus Notes Tools For Faster Notes Development and Better Domino Administration

Tags

Site Info