05/19/2009

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

08/05/2005

Application broken on August, 1st

Tags: JavaScript
Today I received a phone call from one of our customers. I had built an enhancement for the CMS templates used for the customer's website - www.mainz.de. I had tested it accurately and sent an email to the customer to have them test it also.

The new feature enhances the option to maintain a list of links (teasers) on the home page and the main entry pages of the website: they can now set a start time and an end time for each link/teaser. The dates will be monitored for each access to the web pages, to have a very accurate control over the "hot topics". You can also preview the pages for a user specified date and time, to simulate the homepage/teaser list for some time in the future.

Today nothing worked at all. The teaser lists were gone completely.
Last week everything worked.

Good thing: I had installed this on the test system. Seems to be a good approach.

After a lot of searching I found out what's going on: we are using server based JavaScript (embedded in a servlet environment) to render the pages (i.e. TMLScript of WebGate Anywhere 3.1.1). I had to do some date calculations, getting text from the page documents. Therefore I'm getting the date in text format (string), converting this (via parseInt) to integer to create Date objects.

I had not read the complete definition of parseInt: if parseInt encounters numbers with leading zeroes, it will consider them to be octal values instead of decimal.

So for July everything worked well:
parseInt ("07")= = 7, and
parseint ("29") == 29.

but parseInt ("08") == NaN, because there is no 8 in octal numbers....

Solution: parseInt ("08", 10) == 8

Easy enough. I'm happy this happened in the test system, and that it happened now - in 2 weeks I'm off for holiday, and I'd rather not think about the homepage of this (very heavy used) website being completely without contents....

Search

Calendar

MiscLinks

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

Tags

Site Info