The previous section dealt primarily with validating strings; albeit, some of the strings were composed entirely of digits. These were, however, still strings since the results of any mathimatical operation using them is meaningless. Now our attention is turned to numerical data.

Numerical data is alway handled as string representations of numbers in (X)HTML forms or when the data is sent to the server. This allows some string based tests to verify that the string's pattern matches acceptable numeric data.

Only Digits  

A basic test: is there data and is it only digits. Said another way: only an unsigned (positive) whole number.

Enter Test Data
Select an Option and Click Validate



The following three different formats are demonstrated

                function test1(str) {
                    return /^ *[0-9]+ *$/.test(str);
                }
                function test2(str) {
                    return /^\d+$/.test(str);
                }
                function test3(str) {
                    return /^\d{3,5}$/.test(str);
                }

The first regular expression allows one or more characters in the set [0-9] preceded or followed by any number of spaces within the length of the string. The metacharacters ^ and $ meaning beginning and end of string respectively excludes any characters other than specified in the pattern. A number with leading and trailing spaces can be considered valid because conversion to numeric data will ignore the spaces. However, it is a good idea to trim data before validating using alltrim covered previously.

The second pattern disallows leading and trailing spaces and is shorter because in uses the metacharacter \d for digits. If the data is trimmed before validating with an alltrim covered previously, data otherwise valid will pass this test.

The third regular expression is the same as the second with the addition of {3,5} that restricts the data to having 3 to 5 of the preceding character(s) in this case digits.

A signed integer  

Of course, positive and negative integers are common, and positve integers frequently omit the sign so this requires a little consideration.

Enter Test Data
Select an Option and Click Validate



A valid integer value should contain only digits and possibly a leading minus or plus sign. A regular expression to check that would look like this:

            function test1(str) {
                str = alltrim(str);
                return /^[-+]?[0-9]+$/.test(str);
            }
            function test2(str) {
                str = alltrim(str);
                return /^[-+]?\d+$/.test(str);
            }
            function test3(str) {
                str = alltrim(str);
                return /^[-+]?\d{3,5}$/.test(str);
            }

These regular expressions are similar to the three above. The only changes are: a call to alltrim an expression covered earlier and the symbols [-+]? for zero or one of the set minus and plus.

Decimal or Float  

Decimals or floats as they are commonly called present additional considerations. There may be a sign, some whole numbers, a decimal point, and decimal digits.

Enter Test Data
Select an Option and Click Validate




function test1(str) {
    str = alltrim(str);
    return /^[-+]?[0-9]+(\.[0-9]+)?$/.test(str);
}
function test2(str) {
    str = alltrim(str);
    return /^[-+]?\d+(\.\d+)?$/.test(str);
}
function test3(str) {
    str = alltrim(str);
    return /^[-+]?\d{3,5}(\.\d{1,3})?$/.test(str);
}

The expressions again follow the pattern set forth in the previous examples. These differ only in the addition of test, (\.\d+) for a decimal point and decimals.

NOTE: the if a decimal point is present it must be followed by at least one digit in each example.

The third example, limiting the number of decimals, is useful for currencies.

Currency  

Data representing money are generally handled as floats since it is customary to leave out formatting characters. Yet, a developer may have an occasion where accepting formatted currency is required.

Enter Test Data
Select an Option and Click Validate




These expressions expand on and specialize from the previous examples.

function test1(str) {
    str = alltrim(str);
    return /^\$?[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{2})?$/.test(str);
}
function test2(str) {
    str = alltrim(str);
    if (/^\$?[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{0,2})?$/.test(str) ) {
        if (/\.[0-9]$/.test(str) ) {
            str += "0";
        }
        else if (/\.$/.test(str)) {
            str += "00";
        }
        else if (!/\.[0-9]{2}$/.test(str) ) {
            str += ".00";
        }
        return str;
    }
    else {
        return "Invalid";
    }
}
function test3(str) {
    str = alltrim(str);
    if (/^[-+]?\$?[1-9]\d{0,2}(,\d{3})*(\.\d{0,2})?$/.test(str) ) {
        if (/\.\d$/.test(str)) {
            str += "0";
        }
        else if (/\.$/.test(str) ) {
            str += "00";
        }
        else if (!/\.\d{2}$/.test(str) ) {
            str += ".00";
        }
        return str;
    }
    else {
        return "Invalid";
    }
}

There is nothing really tricky in the first regular expression. There is an optional leading dollar sign. It requires the first digit not to be zero, and that can be followed by zero, one, or two digits. After that there can be zero or more groups composed of a comma and exactly three digits. It ends with an optional group composed of a decimal and two digits.

The second regular expression is nearly the same as the first with one difference. It not only allows leaving off the cents, but will accept part of a decimal: the decimal point followed by zero, one, or two digits. If the number is valid, the function then determines what part if any of the decimal is missing and adds the necessary characters (zeroes and decimal point).

The last expression is the same as the second replacing [0-9] with \d.