1 | # inputmask-core [![Build Status](https://secure.travis-ci.org/insin/inputmask-core.png)](http://travis-ci.org/insin/inputmask-core)
|
2 |
|
3 | A standalone input mask implementation, which is independent of any GUI.
|
4 |
|
5 | `InputMask` encapsulates editing operations on a string which must conform to a
|
6 | fixed-width pattern defining editable positions and the types of data they may
|
7 | contain, plus optional static characters which may not be edited.
|
8 |
|
9 | ## Install
|
10 |
|
11 | ```
|
12 | npm install inputmask-core
|
13 | ```
|
14 |
|
15 | ## Usage
|
16 |
|
17 | Importing and creating an instance:
|
18 |
|
19 | ```javascript
|
20 | var InputMask = require('inputmask-core')
|
21 |
|
22 | var mask = new InputMask({pattern: '11/11/1111'})
|
23 | ```
|
24 |
|
25 | Examples of editing a mask:
|
26 |
|
27 | ```javascript
|
28 | /* Invalid input is rejected */
|
29 | mask.input('a')
|
30 | // → false
|
31 |
|
32 | /* Valid input is accepted */
|
33 | mask.input('1')
|
34 | // → true
|
35 | mask.getValue()
|
36 | // → '1_/__/____'
|
37 |
|
38 | /* Editing operations update the cursor position */
|
39 | mask.selection
|
40 | // → {start: 1, end: 1}
|
41 |
|
42 | /* Pasting is supported */
|
43 | mask.paste('2345678')
|
44 | // → true
|
45 | mask.getValue()
|
46 | // → '12/34/5678'
|
47 |
|
48 | /* Backspacing is supported */
|
49 | mask.backspace()
|
50 | // → true
|
51 | mask.getValue()
|
52 | // → '12/34/567_'
|
53 |
|
54 | /* Editing operations also know how to deal with selected ranges */
|
55 | mask.selection = {start: 0, end: 9}
|
56 | mask.backspace()
|
57 | // → true
|
58 | mask.getValue()
|
59 | // → '__/__/____'
|
60 | ```
|
61 |
|
62 | ## API
|
63 |
|
64 | See the [Types](#types) section below for type definitions referenced in this
|
65 | section.
|
66 |
|
67 | ## `InputMask(options: `[`InputMaskOptions`](#inputmaskoptions)`)`
|
68 |
|
69 | Constructs a new `InputMask` - use of `new` is optional, so these examples are
|
70 | equivalent:
|
71 |
|
72 | ```javascript
|
73 | var mask = new InputMask({pattern: '1111-1111', value: '12345678'})
|
74 | ```
|
75 | ```javascript
|
76 | var mask = InputMask({pattern: '1111-1111', value: '12345678'})
|
77 | ```
|
78 |
|
79 | ## `InputMask` public properties, getters & setters
|
80 |
|
81 | ### `selection` : `Selection`
|
82 |
|
83 | The current selection within the input.
|
84 |
|
85 | If `start` and `end` are the same, this indicates the current cursor position in
|
86 | the string, otherwise it indicates a range of selected characters within the
|
87 | string.
|
88 |
|
89 | `selection` will be updated as necessary by editing methods, e.g. if you
|
90 | `input()` a valid character, `selection` will be updated to place the cursor
|
91 | after the newly-inserted character.
|
92 |
|
93 | If you're using `InputMask` as the backend for an input mask in a GUI, make
|
94 | sure `selection` is accurate before calling any editing methods!
|
95 |
|
96 | ### `setSelection(selection: Selection)` : `boolean`
|
97 |
|
98 | Sets the selection and performs an editable cursor range check if the selection
|
99 | change sets the cursor position (i.e. `start` and `end` are the same).
|
100 |
|
101 | If the mask's pattern begins or ends with static characters, this method will
|
102 | prevent the cursor being placed prior to a leading static character or beyond a
|
103 | tailing static character. Only use this method to set `selection` if this is
|
104 | the behaviour you want.
|
105 |
|
106 | Returns `true` if the selection needed to be adjusted as described above,
|
107 | `false` otherwise.
|
108 |
|
109 | ### `getValue()` : `string`
|
110 |
|
111 | Gets the current value in the mask, which will always conform to the mask's
|
112 | pattern.
|
113 |
|
114 | ### `setValue(value: string)`
|
115 |
|
116 | Overwrites the current value in the mask.
|
117 |
|
118 | The given value will be applied to the mask's pattern, with invalid - or
|
119 | missing - editable characters replaced with placeholders.
|
120 |
|
121 | The value may optionally contain static parts of the mask's pattern.
|
122 |
|
123 | ### `setPattern(pattern: string, value: ?string)`
|
124 |
|
125 | Overwrites the mask's pattern.
|
126 |
|
127 | A new value can also be provided - if not provided, the value will default to
|
128 | blank, clearing the mask.
|
129 |
|
130 | ## `InputMask` editing methods
|
131 |
|
132 | Editing methods will not allow the string being edited to contain invalid
|
133 | values according to the mask's pattern.
|
134 |
|
135 | Any time an editing method results in either the value or the selection
|
136 | changing, it will return `true`.
|
137 |
|
138 | Otherwise, if an invalid (e.g. trying to input a letter where the pattern
|
139 | specifies a number) or meaningless (e.g. backspacing when the cursor is at the
|
140 | start of the string) editing operation is attempted, it will return `false`.
|
141 |
|
142 | ### `input(character: string)` : `boolean`
|
143 |
|
144 | Applies a single character of input based on the current selection.
|
145 |
|
146 | * If a text selection has been made, editable characters within the selection
|
147 | will be blanked out, the cursor will be moved to the start of the selection
|
148 | and input will proceed as below.
|
149 |
|
150 | * If the cursor is positioned before an editable character and the input is
|
151 | valid, the input will be added. The cursor will then be advanced to the next
|
152 | editable character in the mask.
|
153 |
|
154 | * If the cursor is positioned before a static part of the mask, the cursor will
|
155 | be advanced to the next editable character.
|
156 |
|
157 | After input has been added, the cursor will be advanced to the next editable
|
158 | character position.
|
159 |
|
160 | ### `backspace()` : `boolean`
|
161 |
|
162 | Performs a backspace operation based on the current selection.
|
163 |
|
164 | * If a text selection has been made, editable characters within the selection
|
165 | will be blanked out and the cursor will be placed at the start of the
|
166 | .selection
|
167 |
|
168 | * If the cursor is positioned after an editable character, that character will
|
169 | be blanked out and the cursor will be placed before it.
|
170 |
|
171 | * If the cursor is positioned after a static part of the mask, the cursor will
|
172 | be placed before it.
|
173 |
|
174 | ### `paste(input: string)`: `boolean`
|
175 |
|
176 | Applies a string of input based on the current selection.
|
177 |
|
178 | This behaves the same as - and is effectively like - calling `input()` for each
|
179 | character in the given string *with one key difference* - if any character
|
180 | within the input is determined to be invalid, the entire paste operation fails
|
181 | and the mask's value and selection are unaffected.
|
182 |
|
183 | Pasted input may optionally contain static parts of the mask's pattern.
|
184 |
|
185 | ## Types
|
186 |
|
187 | These type definitions are purely for reference, they're not part of the
|
188 | API exported by this module.
|
189 |
|
190 | ### `Selection` : `{start: number; end: number}`
|
191 |
|
192 | An object with `start` and `end` properties, where `end >= start`.
|
193 |
|
194 | ### `InputMaskOptions`
|
195 |
|
196 | #### `{pattern: string, value: ?string, selection: ?Selection}`
|
197 |
|
198 | Options for the `InputMask` constructor.
|
199 |
|
200 | #### `pattern`
|
201 |
|
202 | A masking pattern. The following characters signify editable parts of the mask:
|
203 |
|
204 | * `1` - number
|
205 | * `A` - letter
|
206 | * `*` - alphanumeric
|
207 |
|
208 | All other characters are treated as static.
|
209 |
|
210 | A pattern must be provided and must contain at least one editable character, or
|
211 | an `Error` will be thrown.
|
212 |
|
213 | ##### Example patterns
|
214 |
|
215 | * Credit card number: `1111 1111 1111 1111`
|
216 | * Date: `11/11/1111`
|
217 | * ISO date: `1111-11-11`
|
218 | * Time: `11:11`
|
219 | * Canadian postal code: `A1A 1A1`
|
220 | * Norn Iron license plate: `AAA 1111`
|
221 |
|
222 | #### `value`
|
223 |
|
224 | An optional initial value for the mask - see [`setValue()`](#setvaluevalue-string)
|
225 | above for more details.
|
226 |
|
227 | #### `selection`
|
228 |
|
229 | An optional default selection - see [`selection`](#selection-selection)
|
230 | above for more details.
|
231 |
|
232 | ## MIT Licensed |
\ | No newline at end of file |