JavaScript/jQuery Validator - CompareRule
A validation rule that requires validated values to match a specified expression.
To specify a comparison expression, define the comparisonTarget function. Validator compares values to this function's return value. To configure which comparison operator Validator compares values to, specify the comparisonType property.
- $(function() {
- $('#password').dxTextBox({ ... });
- $('#confirm-password').dxTextBox({ ... })
- .dxValidator({
- type: 'compare',
- comparisonTarget() {
- const passwordTextBox = $('#password').dxTextBox('instance');
- if (passwordTextBox) {
- return passwordTextBox.option('value');
- }
- return null;
- },
- message: 'Passwords do not match.',
- });
- });
See Also
comparisonTarget
Specifies the function whose return value is used for comparison with the validated value.
A value to be compared with the validated value.
The rule is valid if the comparison between the validated value and the return value of the comparisonTarget function evaluates to true. The operator specified by the comparisonType property is used to compare values.
comparisonType
Specifies the operator to be used for comparing the validated value with the target.
message
Specifies the message that is shown if the rule is broken.
An error message can be specified as follows:
Hard-code the message
index.js- $(function() {
- $("#textBox").dxTextBox({ ... })
- .dxValidator({
- type: "compare",
- message: "My custom message"
- });
- });
Hide the message
index.js- $(function() {
- $("#textBox").dxTextBox({ ... })
- .dxValidator({
- type: "compare",
- message: ""
- });
- });
Display the editor's name in the message
index.js- $(function() {
- $("#textBox").dxTextBox({ ... })
- .dxValidator({
- name: "Passwords", // The error message will be "Passwords do not match"
- validationRules: [{
- type: "compare"
- }]
- });
- });