Supplied Validators
Requires Platform 4.5+
Supplied Validators
This documet describes the use of the following supplied validators:
Validator | Description |
requiredFieldValidator | Validates whether a field contains a value. |
stringLengthValidator | Validates whether a field contains a value within the specified length range. This validates based on amount of data entered. |
integerValidator | Validates whether a field contains valid whole numbers (integers). |
decimalValidator | Validates whether a field contains valid decimal or floating point numbers. |
rangeValidator | Validates whether a field contains the specified range of integers or decimal numbers. This validates based on actual values of data entered. |
dateValidator | Validates whether a field contains a value in the specified date format. |
currencyValidator | Validates whether a field contains a valid currency amount according to a specified locale. |
validatorChain | Specifies a series of validators to run on one |
Common Attributes
All supplied validators provide the following common attributes:
Validator Tag Element | Description |
id=“passwordValidator” | Specifies a unique ID for this instance of this type of validator. |
value=“password.text” | Specifies the ID of a textField or passwordField in the UI file and the text attribute of that field to validate. |
onSuccess=“mco://myMco.validPassword()” | Specifies the MCO to run - for example, to display a success message - upon successful validation. |
onFailure=“mco://myMco.invalidPassword()” | Specifies the MCO to run - for example, to display a failed message - upon unsuccessful validation. |
For example:
Validator Declaration
<validator:requiredFieldValidator xmlns="http://nexaweb.com/validator" id="usernameValidator" value="username.text" onSuccess="macro://success.execute()" onFailure="macro://failure.execute()" />
Element to Validate
<textField id="username" text="" />
stringLengthValidator
In addition to the attributes common to all validators, the stringLengthValidator provides the following attributes:
Attribute | Description |
maximum = | Maximum number of characters a field can contain. |
minimum = | Minimum number of characters a field can contain. |
Maximum and minimum values include the value specified.
You can use an open ended range by specifying only a maximum or minimum value.
If you do not specify a minimum or maximum value, the element on which you invoke the validator accepts data of any length.
For example:
The following stringLengthValidator validates a string from 8 to 15 characters in length.
Validator Declaration
<validator:stringLengthValidator xmlns:validator="http://nexaweb.com/validator" id="passwordValidator" value="password.text" minimum="8" maximum="15" onSuccess="mco://myMco.validPassword()" onFailure="mco://myMco.invalidPassword()"/>
Element to Validate
<passwordField id="password" text="" />
Specify a minimum attribute value of 8 to create an open-ended range of at least 8 characters.
Specify a maximum attribute value of 8 to create an open-ended range of no more than 8 characters.
Specify minimum and maximim values of 8 to validate for a value length of exactly 8 characters.
rangeValidator
In addition to the attributes common to all validators, the rangeValidator provides the following attributes:
Attribute | Description |
locale | Specifies the locale to use in number formatting. Specify locale value in language-code_country-code format. For language, use a 2-character lower case code as defined by ISO-639. See http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt for a full list of language codes. Default value is en or English. For country, use a 2-character upper case code as defined by ISO-3166. See http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html for A full list of codes. Default value is US or United States. |
maximum | Maximum interger or decimal number the field can contain. |
minimum | Minimum interger or decimal number the field can contain. |
format | Use for ranges of floating point numbers to specify the precision of real numbers. Format must conform to the java.text.DecimalFormat format styles. If you do not specify a format for floating point numbers, validation might result in unexpected behavior. |
Maximum and minimum values include the value specified.
You can use an open ended range by specifying only a maximum or minimum value.
For example:
Validator Declaration
<validator:rangeValidator xmlns:validator=http://nexaweb.com/validator id="weightValidator" value="weight.text" minimum="2.5" maximum="5.8" format="0.0" locale="en_US" onSuccess="mco://myMco.validWeight()" onFailure="mco://myMco.invalidWeight()" />
Element to Validate
<textField id="weight" text="" />
dateValidator
In addition to the attributes common to all validators, the dateValidator provides the following attributes:
Attribute | Description |
format | A valid java.text.SimpleDate format style to define acceptable date format. |
locale | Specifies the locale to use in date formatting. Specify locale value in language-code_country-code format. For language, use a 2-character lower case code as defined by ISO-639. See http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt for a full list of language codes. Default value is en or English. For country, use a 2-character upper case code as defined by ISO-3166. See http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html for A full list of codes. Default value is US or United States. |
For example:
Validator Declaration
<validator:dateValidator xmlns=http://nexaweb.com/validator id="birthValidator" value="birthdate.text" format="yyyy/MM/dd" locale="es_ES" onSuccess="mco://myMco.validBirthdate()" onFailure="mco://myMco.invalidBirthdate()" />
Element to Validate
<textField id="birthdate" text="" />
currencyValidator
In addition to the attributes common to all validators, the currencyValidator provides the following attributes:
Attribute | Description |
locale | Specifies the locale to use in currency formatting. Specify locale value in language-code_country-code format. For language, use a 2-character lower case code as defined by ISO-639. See http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt for a full list of language codes. Default value is en or English. For country, use a 2-character upper case code as defined by ISO-3166. See http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html for A full list of codes. Default value is US or United States. |
prefix | Specifies a prefix that must appear for any input values. For example, $ |
Example
Validator Declaration
<validator:dateValidator xmlns:validator=http://nexaweb.com/validator id="totalValidator" value="total.text" prefix="¥" locale="ja_JP" onSuccess="mco://myMco.validTotal()" onFailure="mco://myMco.invalidTotal()" />
Element to Validate
<textField id="total" text="" />
Validator Chain
You can create a validatorChain to run a series of validators on the same or different fields in a UI file.
The validatorChain provides a failFast boolean attribute, which when set to true, causes the validator chain to stop executing as soon as any validator fails. You can specify onSuccess and onFailure behaviors for the chain in addition to setting these for each validator included in the chain. The chain succeeds if all the validators within it succeed.
If you set onSuccess and onFailure behaviors for the chain in addition to any or all of the validator included in the chain, users can potentially see messages for the success or failure of each validator in addition to seeing messages for the success or failure of the chain.
You can include in a validatorChain a:
- Previously declared validator
- Validator declared for first time in chain
- Validator reference.
The validatorChain element tag includes the following attributes:
<val:validatorChain xmlns:validator=http://nexaweb.com/validator id="loginValidator" onSuccess="mco://myMco.login()" onFailure="macro://failure.execute('invalid username and or password')" failFast= > <validator:Validator /> <validator:validatorReference /> </validator:validatorChain>
The following table provides descriptions of the validatorChain attributes:
Validator Tag Element | Description |
<validator:validatorChain xmlns:val=http://nexaweb.com/validator | Opening tag of a validator chain. |
id=“ ” | Specifies a unique ID for this validator chain. |
onSuccess=“ ” | Specifies the MCO or Macro to run - for example, to display a success message - upon successful completion of all of the validators in the chain. |
onFailure=“ ” | Specifies the MCO or Marco to run - for example, to display a failed message - upon failure of any validator in the chain. |
failFast= | Specifies whether to stop execution of the chain upon failure of any validator within the chain. Set this attribute to change from default only. Default=true. If set to false, validator chain continues to execute until all validators within the chain execute no matter the result of any single validator in the chain. |
<validator:Validator /> or <validator:validatorReference /> | Specifies each validator or reference to a validator to include in the chain. |
</validator:validatorChain> | Closing tag of a validator chain. |
Example:
Validator Declaration
<validator:requiredFieldValidator xmlns:validator="http://nexaweb.com/validator" id="passwordRequiredValidator" value="password.text" onSuccess="" onFailure="macro://failureMacro.execute('Password required')" />
Validator Chain Declaration
<validator:validatorChain xmlns:validator="http://nexaweb.com/validator"> id="myValidator"> <validator:validatorReference validator="passwordRequiredValidator" /> <validator:stringLengthValidator value="password.text" minimum="8" maximum="20" onSuccess="mco://myMco.login()" onFailure="macro://failureMacro.execute( 'Password must be between 8 and 20 character')" /> </validator:validatorChain>
Element to Validate
<textField id="password" text="" onTextChange="validator://myValidator.execute()" />
In this example, the validatorChain, myValidator, includes two validators: the passwordRequiredValidator, declared outside of the chain and specified by reference; and the stringLengthValidator, declared inside the chain only.
Validator Reference
You can reference a previously declared validator in a validatorChain using a validator reference as a shorcut syntax.
The validatorReference tag element appears as follows
<validator:validatorReference xmlns:validator="http://nexaweb.com/validator" validator="usernameValidator" />
The following table provides descriptions of the validatorReference attributes:
Validator Tag Element | Description |
<validator:validatorReference xmlns:validator=“http://nexaweb.com/validator” | Opening tag of for validator reference. |
validator=“ ” | Specifies ID of validator declared in this UI file. |