Number validator

Test a string value (formatted as a JSF number value) or a JavaScript™ number object to see if it conforms with a set of JSF validation constraints.

The NumberValidator is used to test that a value conforms to a set of validation constraints. The validator can be passed a JavaScript number or it can be passed a string which is converted to a number using the supplied NumberConverter (if the conversion fails, the value is invalid).

Validation constraints are supplied when the validator is constructed (or they can be added/removed using the Get/SetAttribute calls). Any number of constraints may be supplied. Supplying no constraints is not meaningful.

If the required constraint is supplied (and set to true), the validator requires that the value be non-null (if a number is provided) or a non-empty string (if a string is supplied). If required is not set, null/empty string are considered valid values regardless of any other constraints (that is "null" is always a valid value if required is not set).

If a minimum-bound is specified as a constraint, a value (if not null) must be greater than or equal to the specified bound. If a maximum bound is specified as a constraint, the value must be less than or equal to the maximum bound.

If a modulus is specified, the digits of the number without regard to grouping/decimal characters are used in the modulus test. The last digit is the check digit and the other other digits processed by the modulus algorithm to test if they "sum" to to the check digit. This constraint can be used to verify that a 16 digit number is a valid credit card number or to verify numbers such as account numbers.

A JavaScript EL expression can be used to test that a value is within a set, to test if a value is in a discontinuous range, and so on. Currently it can not be used to compare one value with another value on the page.

Although it doesn't affect the result of validation, constraints are tested in the following order:
  1. required
  2. minimum-bound
  3. maximum-bound
  4. constraint-expression
  5. modulus

JavaScript constructor

hX_5.addValidator("id", new hX_5.NumberValidator(attributes)); where

id

The ID of the HTML tag to which the component is attached.

Attributes

Table 1. Number validator

Attribute name

Description

required

If required, the value may not be null or the empty string. If not required, the value may be null or the empty string and the other validation constraints are skipped.

minimum-bound

The minimum allowed value. The bounds should be formatted as yyyyMMddHHmmssSSS where trailing components may be omitted.

maximum-bound

The maximum allowed value. The bounds should be formatted as yyyyMMddHHmmssSSS where trailing components may be omitted.

modulus

Value must pass the IBM® Modulus 10 (value is 10) or IBM Modulus 11 (value is 11) self-check algorithm. See http://publib.boulder.ibm.com/iseries/v5r2/ic2924/index.htm?info/rzakc/rzakcmstdfcheck.htm (search for "M10/M10F").

constraint-expression

A JavaScript EL expression that must be satisfied. A JavaScript EL expression is the same as a JSF EL expression with the following exceptions:
  • Operators may not be expressed using the "string" form of the operator (for example, use == not .eq.)
  • The only value that can be referenced is @testValue which represents the value being tested.
  • Date/time components within @testValue can be accessed using the Java™ accessors. Specifically, .date, .day, .hours, .minutes, .month, .seconds, .timezoneOffset, .time and .year may be used.

API calls

Table 2. API calls

API call

Description

boolean = validate(value, converter)

Validate a value (either a string or a date object). Requires either a converter object or the ID of a converter. Returns true if value passes. Returns false if value fails and lastError is set.

string = lastError()

If a conversion fails, returns the reason for the failure as a localized string.

object = setAttribute(attribute)

Sets an attribute or changes its value (if it was set previously).

string = getAttribute(attribute-name)

Retrieves the current value of an attribute.

Limitations

Example code

Validate the value of a pair of input fields.

// Construct a converter with a pattern of $ ###,##0.00;($ ###,##0.00)
hX.addConverter("AZ", new hX.NumberConverter("pattern:$ ###,##0.00;($ ###,##0.00)", 
                  "strict:2", "locale:,.%‰-$"));
// Construct a converter with a pattern of 0000,0000,0000,0000
hX.addConverter("BZ", new hX.NumberConverter("pattern:0000,0000,0000,0000", 
                  "strict:2", "locale:--XXXX"));

// Construct a validator that checks that a field is present and between -100,000 and 100,000 but not zero
hX.addValidator("1Z", new hX.NumberValidator("required:true", 
    "constraint-expression:(@testValue >= -100000 && @testValue <= 100000 && @testvalue != 0)"));
// Construct a validator that checks that a field is present and a valid credit card number
hX.addValidator("2Z", new hX.NumberValidator("required:true", "modulus:10"));

// Validate a pair of values
var x = document.getElementById("form1:textA");
var v = hX.getValidatorById("1Z");
if (!v.validate(x.value, "AZ")) alert ("ERROR: " + v.lastError());
x = document.getElementById("form1:textB");
v = hX.getValidatorById("2Z");
if (!v.validate(x.value, "BZ")) alert ("ERROR: " + v.lastError());

Feedback