GC Design SystemComponents
GC Design System Start to use Page templates overviewBasic page Components overviewAlertBreadcrumbsButtonCardCheckboxesContainerDate inputDate modifiedDetailsError messageError summaryFieldsetFile uploaderFooterGridHeaderHeadingIconInputLanguage toggleLinkNoticePaginationRadiosScreenreader-onlySearchSelectSide navigationSignatureStepperTableTextTextareaTheme and topic menuTop navigation CSS Shortcuts overview Reset styles Responsive layoutState Box sizingContainer sizingDisplayOverflowPositionVisibility FontFont familyFont sizeFont styleFont weightLine heightLink colourLink sizeLink text decorationList styleText alignText colourText overflowText transformWord break MarginPadding Align contentAlign itemsAlign selfFlexFlex directionFlex growFlex shrinkFlex wrapGapGrid columnsGrid rowsJustify contentJustify itemsJustify selfOrderPlace contentPlace itemsPlace self Background colourBorder colourBorder radiusBorder styleBorder width Icon namesIcon sizeImage CursorPointer eventsTransition Styles overviewDesign tokensColourSpacingTypography Contact usGet InvolvedFind a demo

Validate form components

Built-in validation and error messages

GC Design System (GCDS) form components have built-in validation. They validate by default on blur. Fields with specific input requirements will validate if the entry is invalid, even if it is not required. A missing or invalid entry will prompt an inline error message with default text.
ComponentReasonError message
Checkboxes Group: missing value Choose an option to continue.
Single: missing value You must check the box to continue.
Date input Missing all Enter the date.
Missing month Enter the month.
Missing month Select the month.
Missing year Enter the year.
Missing day Enter the day.
Missing month and day Select the month and enter the day.
Missing month and year Select the month and enter the year.
Missing month and day Enter the month and day.
Missing month and year Enter the year and month.
Missing day and year Enter the day and year.
Invalid year length Year must be 4 digits.
Invalid year Enter a valid year.
Invalid month Enter a valid month.
Invalid day Enter a valid day.
Invalid number: min Enter a number that's {min} or more to continue.
Invalid number: max Enter a number that's {max} or less to continue.
File uploader Missing file You must upload a file to continue.
Input Missing or invalid email Enter a valid email address to continue. Use a standard format. Example: name@address.ca.
Missing or invalid url Enter a URL in the specified format to continue.
Invalid format Use the specified format to continue.
Invalid length: too long Enter {max} characters or less to continue. You've entered {current} characters.
Invalid length: too short Enter at least {min} characters to continue. You've entered {current} characters.
Invalid number: min Enter a number that's {min} or more to continue.
Invalid number: max Enter a number that's {max} or less to continue.
Invalid step Enter a number from the specified options to continue. The closest numbers to what you entered are {lower} and {upper}.
Missing number Enter a number to continue.
Missing value Enter information to continue.
Missing email Enter a valid email address to continue. Use a standard format. Example: name@address.ca.
Radios Missing value Choose an option to continue.
Select Missing value Choose an option to continue.
Textarea Invalid format Use the specified format to continue.
Invalid length: too long Enter {max} characters or less to continue. You've entered {current} characters.
Invalid length: too short Enter at least {min} characters to continue. You've entered {current} characters.
Missing value Enter information to continue.

Enable default validation

  • Use the required attribute on form components to activate required field validation.
  • Turn off browser validation by adding novalidate to the form tag.

How validation works

GC Design System form components validate through a series of validation properties, events, and objects.

Validation properties

Validation is controlled by the validator and validate-on properties. | validator | validate-on | | -------- | ------- | | Determines what gets validated. | Determines when the validation check will happen. | | The form component will validate an array of functions in the order they appear. |
  • blur: The check happens the moment a user clicks out of or tabs away from the box.
  • submit: The check happens when the form is submitted.
  • other: The check happens on a different event set by the developer. The developer defines this using the validate() method on each component.
|

Javascript

const gcdsComponent = document.querySelector('gcds-input');

gcdsComponent.validator = [customValidation()];

React

<GcdsInput
  validator={[customValidation()]}
/>

Blur

<gcds-input
  name="blur-validate"
  label="Validate on blur"
  required
  validate-on="blur"
></gcds-input>

Submit

<gcds-input
  name="submit-validate"
  label="Validate on submit"
  required
  validate-on="submit"
></gcds-input>

Other

<gcds-input
  name="other-validate"
  label="Validate on other"
  required
  validate-on="other"
></gcds-input>

Validation events

After running validation, form components generate a gcdsError or gcdsValid event. | gcdsError | gcdsValid | | -------- | ------- | | The data did not pass the checks. | The data passed the checks. | | The event will contain a detail with the associated error message. That error message will be displayed on the component. | An error message will not be displayed. |

Validation objects

Each validate has a validate property that can be called by the form component. The validate property returns an object with multiple properties for the component to read:
  • The valid property contains a boolean value stating if the passed value of the form component passes the validation test.
  • The reason property contains an object with en and fr properties containing the text for the error message in each language.
  • The errors property is an optional property built for more complex form components with multiple form elements that contains an object with boolean properties to inform the form component which element is not valid. The need for this property in a validator will be defined when each form component is created.
// Required field validator used in gcds-input
const requiredField: Validator<string> = {
  validate: (value: string) => {
    return {
      valid: value != null && value.trim() != '',
      reason: {
        en: 'Enter information to continue.',
        fr: 'Saisissez des renseignements pour continuer.',
      },
    };
  },
};

Validator return

interface ValidatorReturn {
  valid: boolean;
  reason: {
    en: string;
    fr: string;
  };
  errors?: object;
}

Validator return with errors

date-input {
  valid: boolean;
  reason: {
    en: string;
    fr: string;
  };
  errors: {
    year: boolean;
    month: boolean;
    day: boolean;
  }
}

Customize validation and error messages

By using a combination of customizable properties and events, you can implement validation that meets your specific needs.
  • Add custom validation using the validator property.
  • Change the validation event using thevalidate-onproperty.
<gcds-input
  input-id="form-name"
  label="Name of component library"
  name="library-name"
  required
></gcds-input>

<script>
  const input = document.querySelector('gcds-input[name="library-name"]');

  const libraryName = () => {
    return {
      validate: value => {
      return {
        valid: value === '@gcds-core/components',
        reason: {
          en: `The entered value must be '@gcds-core/components'.`,
          fr: `La valeur saisie doit être « @gcds-core/components ».`,
        },
      };
      },
    };
  };

  input.validator = [libraryName()];
</script>

Validator format for different components

The format of the validator property will be slightly different for different form components.

Format of the gcds-checkboxes validator:

const validator = () => {
  return {
    validate: value: string[] => {
      return {
        valid: boolean,
        reason: {
          en: string,
          fr: string,
        },
      };
    },
  };
};
</script>

Format of the gcds-date-input validator:

const validator = () => {
  return {
    validate: value: string => {
      return {
        valid: boolean,
        reason: {
          en: string,
          fr: string,
        },
        errors: {
          year: boolean,
          month: boolean,
          day: boolean,
        },
      };
    },
  };
};
</script>

Format of the gcds-file-uploader validator:

const validator = () => {
  return {
    validate: value: FileList => {
      return {
        valid: boolean,
        reason: {
          en: string,
          fr: string,
        },
      };
    },
  };
};
</script>

The validation of gcds-input, gcds-select, gcds-textarea, gcds-radios follow the same pattern.

Format of the their respective validator:

const validator = () => {
  return {
    validate: value: string => {
      return {
        valid: boolean,
        reason: {
          en: string,
          fr: string,
        },
      };
    },
  };
};
</script>
Help us improve Have questions or a request? Give feedback on our contact form. Something's wrong? Raise it through GitHub with an account. You'll have access to the team's direct responses, progress made on your issue, and issues raised by others. Give feedback Report an issue on GitHub
2026-07-20