Skip to content

Date Validation Rules

An accounts payable team rejects invoices dated in the future or more than 90 days ago, and an HR onboarding form needs an end date after the start date. Both rules must hold before a document is submitted, and both must agree with the calendar day the user picked, whatever their time zone.

At a glance

  • Extension point Custom Index Behavior
  • You need date-only DateTime fields InvoiceDate, StartDate and EndDate; permission to edit content types in the Connections app
  • Teaches Reading ISO 8601 date strings, comparing by UTC day, validating one field from two triggers

How date values look

A DateTime or DateTime2 field's RuntimeValue is an ISO 8601 string in UTC, for example "2026-10-07T00:00:00.000Z". A date-only field holds midnight UTC of the chosen day. Compare by calendar day in UTC, as the utcDay helper in both scripts does. Local-time methods such as getDate() can shift the day by one, depending on the time zone of the browser or the server.

Set it up

  1. In the Connections app, open the content type.
  2. Select InvoiceDate. Open Behaviors > Add a Behavior > Custom Behavior, paste Script 1, and select Save & Close.
  3. Select StartDate and add Script 2. Add the same Script 2 to EndDate, because the rule reads both fields.

Scripts

Script 1: Custom Behavior on InvoiceDate
// Attach to: InvoiceDate
// Reads: InvoiceDate. Writes: validity and message of InvoiceDate.
function utcDay(value) {
    // Whole days since 1970-01-01 (UTC), or null if empty or not a date.
    if (!value) { return null; }
    var d = new Date(value);
    if (isNaN(d.getTime())) { return null; }
    return Math.floor(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()) / 86400000);
}

var invoiceDay = utcDay(panel["InvoiceDate"].RuntimeValue);
var today = utcDay(new Date().toISOString());
var message = "";

if (invoiceDay !== null && invoiceDay > today) {
    message = "The invoice date cannot be in the future.";
} else if (invoiceDay !== null && today - invoiceDay > 90) {
    message = "The invoice date cannot be more than 90 days ago.";
}

builder.SetFieldIsValid("InvoiceDate", message === "");
if (message !== "") {
    builder.SetFieldValidationMessage("InvoiceDate", message);
}
Script 2: Custom Behavior on StartDate and EndDate
// Attach to: StartDate and EndDate (same script on both)
// Reads: StartDate, EndDate. Writes: validity and message of EndDate.
function utcDay(value) {
    if (!value) { return null; }
    var d = new Date(value);
    if (isNaN(d.getTime())) { return null; }
    return Math.floor(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()) / 86400000);
}

var start = utcDay(panel["StartDate"].RuntimeValue);
var end = utcDay(panel["EndDate"].RuntimeValue);
var ok = start === null || end === null || end > start;

builder.SetFieldIsValid("EndDate", ok);
if (!ok) {
    builder.SetFieldValidationMessage("EndDate", "The end date must be after the start date.");
}

How it works

  • utcDay turns a date string into a whole day number in UTC, so two dates compare by calendar day and not by time.
  • An empty or unreadable date returns null and passes. Add a Required behavior if the date must have a value.
  • Script 1 picks one message, or none, then sets validity before it adds the message.
  • Script 2 puts the message on EndDate whichever date changed, so the user knows which field to fix.

Test it

  1. In the editor, select Test. Pick tomorrow in InvoiceDate. The field turns red with the future-date message.
  2. Pick a date 100 days ago. The 90-day message shows. Pick today. The message clears.
  3. Pick a StartDate after the EndDate. EndDate turns red. Fix either date. The message clears.

You are done when each rule blocks submit in the Index app and clears as soon as the date is fixed.

What can go wrong

Watch out

  • A date is off by one day. The script uses local-time methods. Use the getUTC... methods for date-only fields.
  • "Today" differs near midnight. The script uses the UTC day. A user west of UTC in the evening is already on the next UTC day. Allow a one-day margin if that matters.
  • A server run marks old invoices invalid. "Today" on the server is the day the step runs. An invoice that waited in a queue can pass in the panel and fail in Run Indexing Behaviors or Start QC Task, and then go to a person. If that is wrong for you, skip the 90-day check when context.app === "worker".
  • Writing a date clears the field and shows "is not a valid date". The control could not parse the string, for example 07.10.2026. Write ISO strings: new Date(Date.UTC(2026, 9, 7)).toISOString().
  • "Could not find localized string." A message was "". Never pass an empty message.

Variations

No weekend dates. new Date(value).getUTCDay() returns 0 for Sunday and 6 for Saturday. Add else if branches for those days.