Class: LengthValidator

Oracle® JavaScript Extension Toolkit (JET)
3.2.0

E87541-01

QuickNav

Fields

oj. LengthValidator

Version:
  • 3.2.0
Since:
  • 0.7

Constructor

new LengthValidator(options)

Constructs a LengthValidator that ensures the value entered is within a given length.

By default this uses Javascript's String length property which counts a UTF-16 surrogate pair as length === 2. If you need to count surrogate pairs as length === 1, then set the countBy option to "codePoint" or use oj.LengthValidator.defaults.countBy = "codePoint"; to set the page-wide default.

You can customize the default messages of all validators including this one using the messageDetail and messageSummary options.

Parameters:
Name Type Argument Description
options Object <optional>
an object literal used to provide:

Properties
Name Type Argument Description
countBy string <optional>
A string that specifies how to count the length. Valid values are "codeUnit" and "codePoint". Defaults to oj.LengthValidator.defaults.countBy which defaults to "codeUnit".
"codeUnit" uses javascript's length function which counts the number of UTF-16 code units. Here a Unicode surrogate pair has a length of two.
"codePoint" counts the number of Unicode code points. Here a Unicode surrogate pair has a length of one.
min number <optional>
a number 0 or greater that is the minimum length of the value.
max number <optional>
a number 1 or greater that is the maximum length of the value.
hint Object <optional>
an optional object literal of hints to be used.

The hint strings (e.g., hint.min) are passed as the 'pattern' parameter to oj.Translations.html#applyParameters. As stated in that documentation, if you are using a reserved character, you need to escape it with a dollar character ('$').

Properties
Name Type Argument Description
max string <optional>
a hint message to be used to indicate the allowed maximum. When not present, the default hint is the resource defined with the key oj-validator.length.hint.max.

Tokens:
{max} - the maximum

Usage:
Enter {max} or fewer characters

min string <optional>
a hint message to be used to indicate the allowed minimum. When not present, the default hint is the resource defined with the key oj-validator.length.hint.min.

Tokens:
{min} the minimum

Usage:
Enter {min} or more characters

inRange string <optional>
a hint message to be used to indicate the allowed range. When not present, the default hint is the resource defined with the key oj-validator.length.hint.inRange.

Tokens:
{min} the minimum

{max} - the maximum

Usage:
Enter between {min} and {max} characters

exact string <optional>
a hint message to be used, to indicate the exact length. When not present, the default hint is the resource defined with the key oj-validator.length.hint.exact.

Tokens:
{length} the length

Usage:
Enter {length} characters

messageDetail Object <optional>
an optional object literal of custom error messages to be used.

The messageDetail strings (e.g., messageDetail.tooLong) are passed as the 'pattern' parameter to oj.Translations.html#applyParameters. As stated in that documentation, if you are using a reserved character, you need to escape it with a dollar character ('$').

Properties
Name Type Argument Description
tooLong string <optional>
the detail error message to be used as the error message, when the length of the input value exceeds the maximum value set. When not present, the default detail message is the resource defined with the key oj-validator.length.messageDetail.tooLong.

Tokens:
{value} - value entered by the user
{max} - the maximum allowed value

Usage:
The {value} has too many characters. Enter {max} or fewer characters, not more.

tooShort string <optional>
the detail error message to be used as the error message, when the length of the input value is less the minimum value set. When not present, the default detail message is the resource defined with the key oj-validator.length.messageDetail.tooShort.

Tokens:
{value} - value entered by the user
{min} - the minimum allowed value

Usage:
The {value} has too few characters. Enter {min} or more characters, not less.

messageSummary Object <optional>
optional object literal of custom error summary message to be used.

The messageSummary strings (e.g., messageSummary.tooLong) are passed as the 'pattern' parameter to oj.Translations.html#applyParameters. As stated in that documentation, if you are using a reserved character, you need to escape it with a dollar character ('$').

Properties
Name Type Argument Description
tooLong string <optional>
the message to be used as the summary error message, when the length of the input value exceeds the maximum value set. When not present, the default message summary is the resource defined with the key oj-validator.length.messageSummary.tooLong.
tooShort string <optional>
the message to be used as the summary error message, when input value is less than the set minimum value. When not present, the default message summary is the resource defined with the key oj-validator.length.messageSummary.tooShort.
Source:

Fields

<static> defaults

The set of attribute/value pairs that serve as default values when new LengthValidator objects are created.

LengthValidator's countBy option may be changed for the entire application after the 'ojs/ojvalidation' module is loaded (each form control module includes the 'ojs/ojvalidation' module). If the options.countBy is specifically set, it will take precedence over this default.

For example: oj.LengthValidator.defaults.countBy = 'codePoint';

Properties:
Name Type Description
countBy string count the length by "codeUnit" or "codePoint". Defaults to "codeUnit".
Since:
  • 2.1.0
Source:

Methods

_getLength() → {number}

Source:
Returns:
the length of the text counted by UTF-16 codepoint or codeunit as specified in the countBy option.
Type
number

getHint() → {String|null}

A message to be used as hint, when giving a hint about the expected length. There is no default hint for this property.
Source:
Returns:
a hint message or null if no hint is available in the options
Type
String | null

Init(options)

Initializes validator instance with the set options
Parameters:
Name Type Argument Description
options Object <optional>
Source:

validate(value) → {string}

Validates the length of value is greater than minimum and/or less than maximum.
Parameters:
Name Type Description
value string that is being validated
Source:
Throws:
when the length is out of range.
Type
Error
Returns:
original if validation was successful
Type
string