Last active
November 21, 2024 13:39
-
-
Save utlime/52a1ac8bb2e2ece1efd20803e693673d to your computer and use it in GitHub Desktop.
Маска ввода для кадастрового номера (text-mask/cadastral-number)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TextMask.maskInput({ | |
inputElement: document.querySelector("input[name = cadastral_number]"), | |
mask: function(rawValue) { | |
var mask = [/\d/, /\d/, ":", /\d/, /\d/, ":"]; | |
var chunks = rawValue.replace(/\_/g, "").split(":"); | |
var i; | |
for (i = 0; (!chunks[2] || i < chunks[2].length) && i < 7; i++) { | |
mask.push(/\d/); | |
} | |
if (!chunks[3] && (!chunks[2] || chunks[2].length < 7)) { | |
mask.push(/\d|:/); | |
} else { | |
mask.push(":"); | |
} | |
for (i = 0; (!chunks[3] || i < chunks[3].length) && i < 20; i++) { | |
mask.push(/\d/); | |
} | |
return mask | |
}, | |
guide: false, | |
keepCharPositions: false, | |
showMask: false | |
}); |
Чуть подредактировал, для использования с https://github.com/vuejs-tips/vue-the-mask и Vue 2
export const CADASTRIAL_MASK = value => {
const mask = ['##:##:']
const chunks = value.replace(/\_/g, '').split(':')
let i
for (i = 0; (!chunks[2] || i < chunks[2].length) && i < 7; i++) {
mask.push('#')
}
if (!chunks[3] && (!chunks[2] || chunks[2].length < 7)) {
mask.push('R')
} else {
mask.push(':')
}
for (i = 0; (!chunks[3] || i <= chunks[3].length) && i < 7; i++) {
mask.push('#')
}
return mask.join('')
}
В компоненте:
computed: {
mask() {
return {
mask: CADASTRIAL_MASK(this.value),
tokens: {
R: {
pattern: /\d|:/
},
'#': { pattern: /\d/ }
}
}
},
...
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Добрый человек, спасибо тебе за готовое решение, которое нагуглилось на русском за 1 минуту. Особо не надеялся на такую удачу. =)
Чутка отрефакторил функцию под свои бизнес-требования: