Final Class: LengthValidator

Oracle® JavaScript Extension Toolkit (JET)
17.0.0

F92240-01

Since:
  • 0.7.0
Module:
  • ojvalidator-length

QuickNav

Fields

Description

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.


Usage

Signature:

final class LengthValidator implements Validator<number|string>

Typescript Import Format
//This class is exported directly as module. To import it
import LengthValidator= require("ojs/ojvalidator-length");

For additional information visit:


Final classes in JET

Classes in JET are generally final and do not support subclassing. At the moment, final is not enforced. However, this will likely change in an upcoming JET release.


See:
  • oj.AsyncLengthValidator

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 oj.LengthValidator.ValidatorOptions <optional>
an object literal used to provide:

See:
  • oj.AsyncLengthValidator

Fields

(static) defaults :object

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/ojvalidator-length' module is loaded (each form control module includes the 'ojs/ojvalidator-length' 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

Methods

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.
Returns:

a hint message or null if no hint is available in the options

Type
string | null

validate(value) : {void}

Validates the length of value is greater than minimum and/or less than maximum.
Parameters:
Name Type Description
value string | number that is being validated
Throws:
when the length is out of range.
Type
Error
Returns:
Type
void

Type Definitions

ValidatorOptions

Properties:
Name Type Argument Description
countBy 'codeUnit' | 'codePoint' <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.
hint Object <optional>
an optional object literal of hints to be used. If not set, defaults will be used for the validator hint. See the individual hint properties below for details.

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
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

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

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

max number <optional>
a number 1 or greater that is the maximum length of the value.
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.
min number <optional>
a number 0 or greater that is the minimum length of the value.