> For the complete documentation index, see [llms.txt](https://docs.smt-x.com/smtx-online-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.smt-x.com/smtx-online-documentation/catalog/catalog-maintenance/add-a-field-to-a-template/field-validation-examples.md).

# Field Validation examples

## **FieldTwo checks if it is equal to FieldOne, if not: show error**&#x20;

1. FieldOne: no validation&#x20;
2. FieldTwo:&#x20;
   1. Set validation error via: Javascript&#x20;
   2. Validation Error:
      1. if (#REPLACE-PARTVALUE-FieldOne||# !== #REPLACE-PARTVALUE-FieldTwo||#) return 'Field one must be equal to field two';&#x20;
   3. Screenshot\
      ![](https://830009223-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXCmB1C0E7jaoFbPx2p%2Fuploads%2FWcRz5ngH4ywKX7iHyWzM%2Fimage.png?alt=media\&token=cb7cc507-405c-4a2c-b34c-2c1087cba9ba)

## **Compare dates**&#x20;

1. Field ‘DateToCompareTo’ contains the date to compare the other field ‘DateAfterDateToCompareWith. ‘DateToCompareTo’ can be filled form the workflow and set as read-only.&#x20;
   1. This is the default value to set on ‘today’:
      1. Default value type: NCALC
      2. Default value: FORMAT('{0:yyyyMMdd}', NOW())&#x20;
2. DateAfterDateToCompareWith&#x20;
   1. Set validation error via: Javascript&#x20;
   2. Validation error:
      1. if (#REPLACE-PARTVALUE-DateAfterDateToCompareWith||# < #REPLACE-PARTVALUE-DateToCompareTo||#) return 'Date must be after date to compare with';&#x20;
      2. Screenshot:\
         ![](https://830009223-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXCmB1C0E7jaoFbPx2p%2Fuploads%2FIGtGy8kitYFZNozSYeVI%2Fimage.png?alt=media\&token=1c80c214-97d8-4562-a2ec-9f3e8bbe1100)
3. DateAfterToday checks if the filled in date is in the future.&#x20;
   1. Set validation error via: NCALC&#x20;
   2. Validation error;
      1. IIF('#REPLACE-PARTVALUE-DateAfterToday||#' < FORMAT('{0:yyyyMMdd}', NOW()), 'Date must be after today', '')

## **Field cannot contain negative values (-)**

1. var thenumber = Number(#REPLACE-PARTVALUE-nummer||#);
2. if (thenumber && thenumber < 0) return "Only positive numbers allowed";

## Make a field required (example is for Document template part)

In the example below, 2 fields are required:

var toreturn = { success: true };

var parts = JSON.parse(g\_data\_string).parts; var category = parts.filter(function (p) { return p.internalname === "Doc\_Category"; }).map(function (p) { return p.value; }).join(""); var version = parts.filter(function (p) { return p.internalname === "Doc\_Versie"; }).map(function (p) { return p.value; }).join(""); if (!category || !version) { toreturn.success = false; toreturn.errormessages = {};

```actionscript
if (!category && !version) {
    toreturn.errormessage = "'Category' and 'Version' are required";    
} else {
    if (!category) {
        toreturn.errormessage = "'Category' is required";
    } else if (!version) {
        toreturn.errormessage = "'Version' is required";
    }
}
if (!category) {
    toreturn.errormessages.Doc_Category = "A value is required";
}
if (!version) {
    toreturn.errormessages.Doc_Versie = "A value is required";
}
```

} return toreturn;

## Make 3 fields required (example is for Document template part)

var toreturn = { success: true };

&#x20;var parts = JSON.parse(g\_data\_string).parts;

var category = parts.filter(function (p) { return p.internalname === "Doc\_Category"; }).map(function (p) { return p.value; }).join("");

var version = parts.filter(function (p) { return p.internalname === "Doc\_Version"; }).map(function (p) { return p.value; }).join("");

var versiondate = parts.filter(function (p) { return p.internalname === "Doc\_Version\_date"; }).map(function (p) { return p.value; }).join("");

if (!category || !version || !versiondate) {

&#x20;   toreturn.success = false;

&#x20;   toreturn.errormessages = {};

&#x20;

&#x20;   var missingfields = \[];

&#x20;   if (!category) {

&#x20;       missingfields.push("Category");

&#x20;       toreturn.errormessages.Doc\_Category = "A value is required";

&#x20;   }

&#x20;   if (!version) {

&#x20;       missingfields.push("Version number");

&#x20;       toreturn.errormessages.Doc\_Version = "A value is required";

&#x20;   }

&#x20;   if (!versiondate) {

&#x20;       missingfields.push("Version date");

&#x20;       toreturn.errormessages.Doc\_Version\_date = "A value is required";

&#x20;   }

&#x20;

&#x20;   toreturn.errormessage = "One or more fields are required: " + missingfields.map(function (fld) { return "'" + fld + "'"; }).join(", ");

}

return toreturn;
