Validation in EJ2 TypeScript Multi select control

2 May 20238 minutes to read

MultiSelect component inside form can be validated through FormValidator. Add the name attribute of component to be validated inside rules of FormValidator. Error message after validation can be placed in DOM based on the requirement through customPlacement.

In the following sample, validation is added for MultiSelect component.

import { enableRipple } from '@syncfusion/ej2-base';
import { FormValidator, FormValidatorModel } from '@syncfusion/ej2-inputs';
import { Button } from '@syncfusion/ej2-buttons';
import { MultiSelect } from '@syncfusion/ej2-dropdowns';

   let listObj1: MultiSelect = new MultiSelect({
        // set the placeholder to MultiSelect input element
        placeholder: 'Favorite Sports',
        // set the type of mode for how to visualized the selected items in input element.
        mode: 'Default'

    // Initialize Submit button
    let buttonFormValidate: Button = new Button({ isPrimary: true });
    // Initialize Reset button
    let buttonReset: Button = new Button({});
    // Initialize Custom placement
    let option: FormValidatorModel = {
        rules: {
            // Initialize the CustomPlacement.
             default: { required: true }
    customPlacement: (inputElement: HTMLElement, error: HTMLElement)=>{
        inputElement.parentElement.parentElement.parentElement.insertBefore(error, inputElement.parentElement.parentElement.nextSibling);
    // Initialize Form validation
    let formObj: FormValidator;
    formObj = new FormValidator('#formId', option);
    let formId: HTMLElement = <HTMLElement>document.getElementById('formId');
        (e: Event) => {
            if (formObj.validate()) {
<!DOCTYPE html>
<html lang="en">

    <title>Essential JS 2 MultiSelect</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript UI Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="styles.css" rel="stylesheet" />
    <link href="" rel="stylesheet" />
    <link href="" rel="stylesheet" />
    <link href="" rel="stylesheet" />
    <link href="" rel="stylesheet" />
    <script src=""></script>
    <script src="systemjs.config.js"></script>
<script src="" type ="text/javascript"></script>

    <div id='loader'>LOADING....</div>
    <div id='container' style="margin:0 auto; width:250px;">
      <div class="col-lg-12 control-section">
    <div class="content-wrapper" style="margin-bottom: 25px;">
        <div class="form-title"><span>Add Customer Details</span></div>
        <form id="formId" class="form-horizontal" novalidate="">
            <div class="form-group">
                <div class="e-float-input">
                <select id="default"  name="default"> 
                <option value="Game1">American Football</option>
                <option value="Game2">Badminton</option>
                <option value="Game3">Basketball</option>
                <option value="Game4">Cricket</option>
                <option value="Game5">Football</option>
                <option value="Game6">Golf</option>
                <option value="Game7">Hockey</option>
                <option value="Game8">Rugby</option>
                <option value="Game9">Snooker</option>
                <option value="Game10">Tennis</option>
                <div id="userError"></div>
            <div class="row">
                <div style="width: 320px;margin:0px auto;height: 100px;padding-top: 25px;">
                    <div style="display: inline-block;">
                        <button id="validateSubmit" class="samplebtn e-control e-btn e-primary" type="submit" style="height:40px;width: 150px;" data-ripple="true">Submit</button>
                    <div style="float: right;">
                        <button id="resetbtn" class="samplebtn e-control e-btn" type="reset" style="height:40px;width: 150px;" data-ripple="true">Clear</button>