site logo

Continental Number Validation

How to validate numbers in web form with reference to national conventions and some other problems of number validation are described in this article. Number validation in browser is performed to prevent from errors on server at string to numeric data type conversion and errors from attempt to save incorrect data type into the database.

Numeric data, which are entered to the textbox of web form, as well as form data which are submited to server are always string data type. As valid numeric data we consider such string, which client and server evaluate as valid number and in addition is in line with data type of database column where data are to be saved.

US notation of float number use as separator of decimal part of number character dot, while continental conventions prescribe to use character comma there. Character comma can be used in Anglo-American countries for separation of groups digit places (thousands). Continental conventions for this case employ character space. JavaScript in functions for type casting from strings to numeric values always requires using decimal dot as Decimal separator and no Thousands separator.

Correct number validation in browser with regard to national conventions of application is necessary, but not sufficient. Request form variables which carry real number numeric string have to be unified on server, to contain proper Decimal Separator. This decimal separator depends on current national environment of server programming language and database setting. Numerical range of integers according to data type of database should be tested in order to avoid overflow errors. Keep in mind, that saving big positive integer number sometimes overflows to the range of negative numbers.

Because some desktop programs require to use decimal point (dot), while others require decimal comma, users are often confused which decimal separator to use. The best practise may be to allow both and for internal calculation and validation unify decimal separator according to system needs. The best help for users to correct format the input numeric string is to keeps consitency in output / input strings.

number type value result
integer number
float number
any number

Test form and scripts

Type numbers to the mini-form and click on button to start validation script for checking of all typed data. JavaScript evaluate numerical data and result notify to in right column of form.

At numbers validation procedure is following: form data, which are always type of character string converted to type numeric by the help of two different method and both numerical values are compared. When values are not identical, initial numerical data are fault.

Some validation methods use one conversion to type numeric, followed by conversion back to type string. Result is compared with initial text string then on coincidence. Such validation operate till then, than somebody leave in form field white character in front of, or behind the number. Because such a space is not visible in textbox, it is very hard to recognize source of error. There have been yet another methods for validation numbers e.g. by the help of regular expression, or by help of function isNaN(), but are not described here.


// ---------- integer number validation -----------
function isInteger(value) {
  if(value == "") return false;
  if(parseInt(value, 10) != (value*1)) return false;
  return true;
}

First sample code for conversion text string to numeric type is by the help of function parseInt(). Conversion is terminated at the end of string, or at match of non-numerical character. Second parameter of function determines base in which conversion proceeds. Skiped parameter of number system can sometimes cause incorrect validation (e.g. numbers beginning with zero are treated as octal base, leading 0x as hexadecimal base).

Further simple arithmetic operation is executed. That operation does not change value, but performs type cast to numeric type. It is possible to use multiplication by one, division by one, or subtraction zero. Care it is not possible to add zero. Operation plus is interpreted as string concatenation, in this case would mean multiplication by ten. Both results, now data type numeric, are compared. As far as are coincident, text parameter is convertible to numerical data and may be regarded as valid number.


// ---------- float number validation -------------
function isFloat(value) {
  if(value == "") return false;
  value = value.replace(/,/, '.');
  if(parseFloat(value) != (value*1)) return false;
  return true;
}

The second code sample validates floating point number. Function allow both scientific and standard style notation. First test, whether string is not empty. By that possible conversion errors of JavaScript in older browsers are avoided. Run time error in IE JavaScript causes break of validation routine and form is sent off as if it had been valid. That would be fundamental fault.

The second line consolidates decimal character so, to was string plain for JavaScript. Relevant decimal comma is replaced by decimal dot. Original string in textbox stay unchanged and in original figure is also submited on server as part of form string. Therefore similar unification of decimal sign we have to perform also on server, this time with reference to national convention of database, so whether is expected decimal point or comma.

Further function proceed in the same way as in previous exhibits only with difference using conversion function parseFloat(). This function does not require base parameter, conversion proceed always in decimal base.


// ---------- any number validation -------------
function isNumber(value) {
  if(value == "") return false;
  value = value.replace(/ /gi, "");
  value = value.replace(/,/, '.');
  oneDeci = false;
  for (var i = 0; i < value.length; i++) {
    var oneChar = value.charAt(i);
    if (i == 0 && oneChar == "-") {
      continue;
    }
    if (oneChar == "." && !oneDeci) {
      oneDeci = true;
      continue
    }
    if (oneChar < "0" || oneChar > "9") {
      return false
    }
  }
  return true;
}

Last example is a bit different way of numeric data validation. Instead of conversion to numeric data type, allowed characters are tested. The second line of code deletes all spaces from input string (thousands separators, leading, closing space). Next line performs potential replacement of decimal comma for point. In the fourth line of code occurrence of decimal separator attribute is reset.

Modified string is passed character by character for test. First character can be negative sign. Further test is decimal point occurrence, which may appear in string only once. All else characters can be digits only.

It is possible to use space as thousands separator and either decimal point or comma in input string. It is not possible to use plus sign for positive numbers, or US thousands separator by comma. Code example fits as basis for experiments with modification of validation function. There are often specific requirements of formating input string numbers (fixed number of decimal digits, positive numbers only, specified range of number) then validation function needs to be modified.

It is possible to replace the last code example by built-in function IsNumeric() in VBScript in similar way as was described in previous article. I do not advise to use VBScript for validation functions in browser, but on server side this old programing language is quite efficient.

updated 30.03.2008