Search.../

onCustomValidation( )

Adds an event handler that runs when the element's validation is checked.

Description

The onCustomValidation() function allows you perform custom validation in addition to any basic validation that was defined in the Editor.

To invalidate the element, call the reject() function that is passed into the validator callback function and pass it a validation message.

The element's validity is checked when the value of the element changes either by user interaction or programmatically.

Note that validations other than required, including custom validations, are not run on input elements when they don't have a value.

You can use the override parameter to override previously applied custom validations.

Authorization

Request

This endpoint does not take any parameters

Status/Error Codes

Was this helpful?

Set an element to invalid if its value is "evil"

Copy Code
1$w("#myElement").onCustomValidation( (value, reject) => {
2 if(value === "evil") {
3 reject("Evil is invalid");
4 }
5} );
Set a new custom validation that overrides a previous validation

Copy Code
1// This validation won't run
2$w("#myElement").onCustomValidation( (value, reject) => {
3 if(value === "evil") {
4 reject("Evil is invalid");
5 }
6}, false);
7
8// This validation will run
9$w("#myElement").onCustomValidation( (value, reject) => {
10 if(value === "bad") {
11 reject("Bad is invalid");
12 }
13}, true);
Set multiple custom validations

In this example, only the 3rd and 4th custom validations run. This is because the override property for the 3rd validation is set to true by default and overrides the previous 2 validations.

Copy Code
1// This validation won't run
2$w("#myElement").onCustomValidation( (value, reject) => {
3 if(value === "ain't") {
4 reject("Ain't is invalid");
5 }
6}, false);
7
8// This validation won't run
9$w("#myElement").onCustomValidation( (value, reject) => {
10 if(value === "i'd've") {
11 reject("I'd've is invalid");
12 }
13}, true);
14
15// This validation runs first
16$w("#myElement").onCustomValidation( (value, reject) => {
17 if(value === "there're") {
18 reject("There're is invalid");
19 }
20});
21
22// This validation runs second
23$w("#myElement").onCustomValidation( (value, reject) => {
24 if(value === "could've") {
25 reject("Could've is invalid");
26 }
27}, false);