Skip to content

Commit 1056f6d

Browse files
author
vagrant
committed
Added the ability to specify an object as a mask
1 parent c387e29 commit 1056f6d

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

src/MaskedInput.js

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ export default {
3535
type: String
3636
},
3737
mask: {
38-
type: String,
3938
required: true,
40-
validator: value => !! (value && value.length >= 1)
39+
validator: value => !! ((value && value.length >= 1) || value instanceof Object)
4140
},
4241
placeholderChar: {
4342
type: String,
@@ -67,30 +66,34 @@ export default {
6766

6867
initMask() {
6968
try {
70-
this.mask_core = new InputMask({
71-
pattern: this.mask,
72-
value: '',
73-
placeholderChar: this.placeholderChar,
74-
formatCharacters: {
75-
'a': {
76-
validate: char => /^[A-Za-zА-Яа-я]$/.test(char),
77-
},
78-
'A': {
79-
validate: char => /^[A-Za-zА-Яа-я]$/.test(char) ,
80-
transform: char => char.toUpperCase()
81-
},
82-
'*': {
83-
validate: char => /^[\dA-Za-zА-Яа-я]$/.test(char),
84-
},
85-
'#': {
86-
validate: char => /^[\dA-Za-zА-Яа-я]$/.test(char),
87-
transform: char => char.toUpperCase()
88-
},
89-
'+': {
90-
validate: char => true,
91-
},
92-
}
93-
})
69+
if (this.mask instanceof Object) {
70+
this.mask_core = new InputMask(this.mask)
71+
} else {
72+
this.mask_core = new InputMask({
73+
pattern: this.mask,
74+
value: '',
75+
placeholderChar: this.placeholderChar,
76+
formatCharacters: {
77+
'a': {
78+
validate: char => /^[A-Za-zА-Яа-я]$/.test(char),
79+
},
80+
'A': {
81+
validate: char => /^[A-Za-zА-Яа-я]$/.test(char),
82+
transform: char => char.toUpperCase()
83+
},
84+
'*': {
85+
validate: char => /^[\dA-Za-zА-Яа-я]$/.test(char),
86+
},
87+
'#': {
88+
validate: char => /^[\dA-Za-zА-Яа-я]$/.test(char),
89+
transform: char => char.toUpperCase()
90+
},
91+
'+': {
92+
validate: char => true,
93+
},
94+
}
95+
})
96+
}
9497
for (let i = 0; i < this.$refs.input.value.length; ++i) {
9598
this.mask_core.input(this.$refs.input.value[i])
9699
}

0 commit comments

Comments
 (0)