Forms Support

You can use the MaskedTextBox in template-driven or reactive forms.

Template-Driven Forms

The template-driven forms enable you to bind the value of the MaskedTextBox to the forms model by using the ngModel directive.

The following example demonstrates how to use the MaskedTextBox in a template-driven form.

@Component({
    selector: 'my-app',
    template: `
        <form #form="ngForm">
            <div class="example-config">
                <pre>{{ value | json }}</pre>
            </div>
            <label>
            Select Gender:
                <kendo-maskedtextbox
                name="masked"
                [mask]="'??????????'"
                [(ngModel)]="value"
            >
            </kendo-maskedtextbox> <br />
            </label>
        </form>
    `
   })
export class AppComponent {
  public value: string;
}

Reactive Forms

The FormGroup provides a way to render reactive forms. For more details, refer to the Angular Documentation.

The following example demonstrates how to use the MaskedTextBox in a reactive form.

import {
       FormsModule,
       ReactiveFormsModule,
       FormGroup,
       FormBuilder
   } from '@angular/forms';

   @Component({
       selector: 'my-app',
       template: `
           <div class="example-config">
               Question mark (?) is a placeholder for a letter or a space.
           </div>
           <form [formGroup]="form" #reactiveForm="ngForm">
               <kendo-maskedtextbox formControlName="masked" [mask]="'??????????'"></kendo-maskedtextbox> <br />
               <p *ngIf="form.controls.masked.errors">{{form.controls.masked.errors | json}}</p>
           </form>
       `
   })
   class AppComponent {
       public form: FormGroup;
       constructor(private formBuilder: FormBuilder) {
           this.form = this.formBuilder.group({
               masked: ["John"]
           });
       }
   }

FormField Association

The following example demonstrates how to use the MaskedTextBox within a FormField component and provide hint and error messages.


import { Component, ViewEncapsulation } from '@angular/core';
import { Validators, FormGroup, FormControl } from '@angular/forms';

@Component({
    selector: 'my-app',
    template: `
        <div class="example">
            <form class="k-form" [formGroup]="form">
                <kendo-formfield [showHints]="'initial'" [showErrors]="'initial'">
                    <kendo-label [for]="phoneNumber" text="Phone Number"></kendo-label>
                    <kendo-maskedtextbox #phoneNumber [formControlName]="'phoneNumber'" [mask]="phoneNumberMask" [value]="phoneNumberValue"></kendo-maskedtextbox>

                    <kendo-formhint [align]="'start'">Hint: Your active phone number</kendo-formhint>
                    <kendo-formerror>
                        <div *ngIf="form.controls.phoneNumber.errors">Error: Not a valid phone number format</div>
                        <div *ngIf="form.controls.phoneNumber.errors?.required">Error: Phone number is required</div>
                    </kendo-formerror>
                </kendo-formfield>
            </form>
        </div>
    `,
    styles: [`
        .example {
            display: flex;
            justify-content: center;
        }
        .k-form { width: 400px; }
    `],
    encapsulation: ViewEncapsulation.None
})

export class AppComponent {
    public phoneNumberValue = '';
    public phoneNumberMask = '(999) 000-00-00-00';

    public form: FormGroup;

    public userData: any = {
        phoneNumber: this.phoneNumberValue
    };

    constructor() {
        this.form = new FormGroup({
            phoneNumber: new FormControl(this.userData.phoneNumber, [Validators.required])
        });
    }
}
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { LabelModule } from '@progress/kendo-angular-label';
import { InputsModule } from '@progress/kendo-angular-inputs';
import { AppComponent } from './app.component';

@NgModule({
    bootstrap:    [AppComponent],
    declarations: [AppComponent],
    imports:      [
        BrowserModule,
        BrowserAnimationsModule,
        CommonModule,
        FormsModule,
        ReactiveFormsModule,
        InputsModule,
        LabelModule
    ]
})
export class AppModule { }
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

enableProdMode();

const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);

In this article