输入掩码

InputMask控件允许您在输入时验证和格式化用户输入,从而防止出现无效数据。

要使用InputMask控件,请将 mask 属性设置为指定每个输入\ position的有效字符类的字符串, 如InputMask文档中所述..

以下是一些示例:

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import * as wijmo from '@grapecity/wijmo'; import * as input from '@grapecity/wijmo.input'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // edit date and time using an InputDateTime create some InputMask controls let theSSN = new input.InputMask('#theSSN', { mask: '000-00-0000' }); // let theZip = new input.InputMask('#theZip', { mask: '00000' }); // let theZip4 = new input.InputMask('#theZip4', { mask: '00000-0000' }); // let thePhone = new input.InputMask('#thePhone', { mask: '(999) 000-0000' }); // // enable overwrite mode on all input mask controls // https://stackoverflow.com/questions/2561110/javascript-to-make-input-field-in-edit-modeinsert-mode var overwrite = document.querySelector('#overwrite'); document.body.addEventListener('keypress', e => { if (overwrite.checked) { // overwrite enabled let input = e.target; // if (input.tagName == 'INPUT') { // target is input element if (wijmo.closest(input, '.wj-inputmask')) { // contained in an InputMask control let value = input.value, start = input.selectionStart; // input.value = value.substr(0, start) + value.substr(start + 1); input.selectionEnd = start; } } } }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity InputMask Overview</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div class="container-fluid"> <div class="form-group"> <label for="theSSN">Social Security Number</label> <input id="theSSN"> </div> <div class="form-group"> <label for="theZip">Zip Code</label> <input id="theZip"> </div> <div class="form-group"> <label for="theZip4">Zip+4 Code</label> <input id="theZip4"> </div> <div class="form-group"> <label for="thePhone">Phone Number</label> <input id="thePhone"> </div> <p> By default, typing into the <b>InputMask</b> inserts text at the cursort position. In some cases, you may want to overwrite the text instead. Click the checkbox below to see the difference: </p> <label> Enable Overwrite <input id="overwrite" type="checkbox"> </label> </div> </body> </html> label { width: 180px; text-align: right; margin-right: 3px; } body { margin-bottom: 48px; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import * as wijmo from '@grapecity/wijmo'; // import { Component, enableProdMode, NgModule } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { WjInputModule } from '@grapecity/wijmo.angular2.input'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { overwrite = false; // constructor() { // enable overwrite mode on all input mask controls // https://stackoverflow.com/questions/2561110/javascript-to-make-input-field-in-edit-modeinsert-mode document.body.addEventListener('keypress', e => { if (this.overwrite) { // overwrite enabled let input = <HTMLInputElement>e.target; // if (input.tagName == 'INPUT') { // target is input element if (wijmo.closest(input, '.wj-inputmask')) { // contained in an InputMask control let value = input.value, start = input.selectionStart; // input.value = value.substr(0, start) + value.substr(start + 1); input.selectionEnd = start; } } } }); } } // @NgModule({ imports: [WjInputModule, FormsModule, BrowserModule], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule { } // enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity InputMask Overview</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Polyfills --> <script src="node_modules/core-js/client/shim.min.js"></script> <script src="node_modules/zone.js/dist/zone.min.js"></script> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.js"></script> <script src="systemjs.config.js"></script> <script> // workaround to load 'rxjs/operators' from the rxjs bundle System.import('rxjs').then(function (m) { System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html> <div class="container-fluid"> <div class="form-group"> <label for="theSSN">Social Security Number</label> <wj-input-mask id="theSSN" [mask]="'000-00-0000'"></wj-input-mask> </div> <div class="form-group"> <label for="theZip">Zip Code</label> <wj-input-mask id="theZip" [mask]="'00000'"></wj-input-mask> </div> <div class=" form-group"> <label for="theZip4">Zip+4 Code</label> <wj-input-mask id="theZip4" [mask]="'00000-0000'"></wj-input-mask> </div> <div class="form-group"> <label for="thePhone">Phone Number</label> <wj-input-mask id="thePhone" [mask]="'(999) 000-0000'"></wj-input-mask> </div> <p> By default, typing into the <b>InputMask</b> inserts text at the cursort position. In some cases, you may want to overwrite the text instead. Click the checkbox below to see the difference: </p> <label> Enable Overwrite <input type="checkbox" [(ngModel)]="overwrite"> </label> </div> label { width: 180px; text-align: right; margin-right: 3px; } body { margin-bottom: 48px; } <template> <div class="container-fluid"> <div class="form-group"> <label for="theSSN">Social Security Number</label> <wj-input-mask id="theSSN" :mask="'000-00-0000'"></wj-input-mask> </div> <div class="form-group"> <label for="theZip">Zip Code</label> <wj-input-mask id="theZip" :mask="'00000'"></wj-input-mask> </div> <div class=" form-group"> <label for="theZip4">Zip+4 Code</label> <wj-input-mask id="theZip4" :mask="'00000-0000'"></wj-input-mask> </div> <div class="form-group"> <label for="thePhone">Phone Number</label> <wj-input-mask id="thePhone" :mask="'(999) 000-0000'"></wj-input-mask> </div> <p> By default, typing into the <b>InputMask</b> inserts text at the cursort position. In some cases, you may want to overwrite the text instead. Click the checkbox below to see the difference: </p> <label> Enable Overwrite <input type="checkbox" v-model="overwrite"> </label> </div> </template> <script> import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import Vue from 'vue'; import '@grapecity/wijmo.vue2.input'; let App = Vue.extend({ name: 'app', data: function () { return { overwrite: false } }, mounted: function(){ document.body.addEventListener('keypress', e => { if (this.overwrite) { // overwrite enabled let input = e.target; // if (input.tagName == 'INPUT') { // target is input element if (wijmo.closest(input, '.wj-inputmask')) { // contained in an InputMask control let value = input.value, start = input.selectionStart; // input.value = value.substr(0, start) + value.substr(start + 1); input.selectionEnd = start; } } } }) } }) let vm = new Vue({ render: h => h(App) }).$mount('#app'); </script> <style> label { width: 180px; text-align: right; margin-right: 3px; } body { margin-bottom: 48px; } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity InputMask Overview</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app.vue'); </script> </head> <body> <div id="app"></div> </body> </html> import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './app.css'; // import * as React from 'react'; import * as ReactDOM from 'react-dom'; // import * as wijmo from '@grapecity/wijmo'; import * as wjInput from '@grapecity/wijmo.react.input'; // class App extends React.Component { constructor(props) { super(props); this.updateOverviewStatus = (target) => { this.setState({ overwrite: target.checked }); }; this.componentDidMount = () => { document.body.addEventListener('keypress', (e) => { if (this.state.overwrite) { // overwrite enabled let input = e.target; if (input.tagName == 'INPUT') { // target is input element if (wijmo.closest(input, '.wj-inputmask')) { // contained in an InputMask control let value = input.value, start = input.selectionStart; // input.value = value.substr(0, start) + value.substr(start + 1); input.selectionEnd = start; } } } }); }; this.state = { overwrite: false }; } ; render() { return <div className="container-fluid"> <div className="form-group"> <label htmlFor="theSSN">Social Security Number</label> <wjInput.InputMask id="theSSN" mask="000-00-0000"> </wjInput.InputMask> </div> <div className="form-group"> <label htmlFor="theZip">Zip Code</label> <wjInput.InputMask id="theZip" mask="00000"> </wjInput.InputMask> </div> <div className=" form-group"> <label htmlFor="theZip4">Zip+4 Code</label> <wjInput.InputMask id="theZip4" mask="00000-0000"> </wjInput.InputMask> </div> <div className="form-group"> <label htmlFor="thePhone">Phone Number</label> <wjInput.InputMask id="thePhone" mask="(999) 000-0000"> </wjInput.InputMask> </div> <p> By default, typing into the <b>InputMask</b> inserts text at the cursort position. In some cases, you may want to overwrite the text instead. Click the checkbox below to see the difference: </p> <label> Enable Overwrite <input type="checkbox" onChange={(e) => { this.updateOverviewStatus(e.target); }} defaultChecked={false}/> </label> </div>; } } ReactDOM.render(<App />, document.getElementById('app')); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>AutoComplete</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div id="app"></div> </body> </html> label { width: 180px; text-align: right; margin-right: 3px; } body { margin-bottom: 48px; }