The following section explains the required steps to build the MaskedTextBox component with its basic usage in step-by-step procedure.
System requirements for Syncfusion Vue UI components
You can use Vue CLI
to setup your vue applications.
To install Vue CLI use the following command.
npm install -g @vue/cli
Start a new project using below Vue CLI command.
vue init webpack-simple quickstart
cd quickstart
npm install
All the available Essential JS 2 packages are published in npmjs.com public registry. You can choose the component that you want to install. For this application, we are going to use MaskedTextBox component.
To install MaskedTextBox component, use the following command
npm install @syncfusion/ej2-vue-inputs –save
For Registering Vue Component two ways are available. They are as follows.
Import the MaskedTextBox Component Plugin from the EJ2 Vue Package and register the same using Vue.use() with MaskedTextBox Component Plugin as its argument.
Refer the code snippet given below.
import { MaskedTextBoxPlugin } from "@syncfusion/ej2-vue-inputs";
Vue.use(MaskedTextBoxPlugin);
Note : By Registering Component Plugin in Vue, all child directives are also globally registered.
Import the MaskedTextBox Component and MaskedTextBox Component Plugin from EJ2 Vue Package, register the same using the Vue.component() with name of MaskedTextBox Component from MaskedTextBox Component Plugin and the EJ2 Vue Component as its arguments.
Refer the code snippet given below.
import { MaskedTextBoxComponent, MaskedTextBoxPlugin } from '@syncfusion/ej2-vue-inputs';
Vue.component(MaskedTextBoxPlugin.name, MaskedTextBoxComponent);
By using Vue.component(), only the EJ2 Vue Component is registered. Child directives needs to be registered separately.
Add the EJ2 Vue MaskedTextBox using <ejs-maskedtextbox>
to the <template>
section of the App.vue
file in src directory,
the content attribute of the MaskedTextBox component is provided as name in data option in the <script>
section.
<template>
<div class="control_wrapper">
<div class="control-label">MaskedTextbox
</div>
<ejs-maskedtextbox></ejs-maskedtextbox>
</div>
</template>
<script>
import Vue from "vue";
import { MaskedTextBoxPlugin } from "@syncfusion/ej2-vue-inputs";
Vue.use(MaskedTextBoxPlugin);
export default Vue.extend ({});
</script>
You can set the mask to the MaskedTextBox to validate the user input by using the mask property. To know more about the usage of mask and configuration, refer to this link.
The following example demonstrates the usage of mask element 0 that allows any single digit from 0 to 9.
<template>
<div class="control_wrapper">
<div class="control-label">MaskedTextBox
</div>
<ejs-maskedtextbox mask="000-000-0000"></ejs-maskedtextbox>
</div>
</template>
<script>
import Vue from "vue";
import { MaskedTextBoxPlugin } from "@syncfusion/ej2-vue-inputs";
Vue.use(MaskedTextBoxPlugin);
export default Vue.extend ({});
</script>
Import the MaskedTextBox component’s required CSS references as follows in src/App.css
.
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-inputs/styles/material.css";
If you want to refer the combined component styles, please make use of our
CRG
(Custom Resource Generator) in your application.
Use the npm run dev
command to run the application in the browser.
npm run dev
The following example shows the MaskedTextBox.
<template>
<div id="app">
<div class='wrap'>
<ejs-maskedtextbox mask='000-000-0000' placeholder='MaskedTextBox' floatLabelType='Always'></ejs-maskedtextbox>
</div>
</div>
</template>
<script>
import Vue from "vue";
import { MaskedTextBoxPlugin } from "@syncfusion/ej2-vue-inputs";
Vue.use(MaskedTextBoxPlugin);
export default {
data () {
return {}
}
}
</script>
<style>
@import "../../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../../node_modules/@syncfusion/ej2-vue-inputs/styles/material.css";
.wrap {
margin: 0 auto;
width: 240px;
}
</style>
In MaskedTextBox, the value
property supports model binding functionality.
The following example demonstrates model binding functionality with the MaskedTextBox and HTML input element.
<template>
<div id="app">
<div class='wrap'>
<div class='e-input-group' style='margin-bottom:30px'>
<input class='e-input' type='text' v-model='value' placeholder='Mobile Number' />
</div>
<ejs-maskedtextbox v-model="value" :value="value" mask='000-000-0000'></ejs-maskedtextbox>
</div>
</div>
</div>
</template>
<script>
import Vue from "vue";
import { MaskedTextBoxPlugin } from "@syncfusion/ej2-vue-inputs";
Vue.use(MaskedTextBoxPlugin);
export default {
data () {
return {
value : ''
}
}
}
</script>
<style>
@import "../../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../../node_modules/@syncfusion/ej2-vue-inputs/styles/material.css";
.wrap {
margin: 35px auto;
width: 240px;
}
</style>