Last active
April 25, 2022 07:16
-
-
Save igorescobar/6450250 to your computer and use it in GitHub Desktop.
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
$(".data").mask("AB/CD/0000", { | |
translation:{ | |
A: {pattern: /[0-3]/}, | |
B: {pattern: /[0-9]/}, | |
C: {pattern: /[0-1]/}, | |
D: {pattern: /[0-9]/}, | |
}, | |
onKeyPress: function(a, b, c, d) { | |
if (!a) return; | |
var m = a.match(/(\d{2})/g); | |
d.translation.B = (m[0] && m[0].charAt(0)=='3') ? { pattern: /[0-1]/ } : { pattern: /[0-9]/ }; | |
d.translation.D = (m[1] && m[1].charAt(0)=='1') ? { pattern: /[0-2]/ } : { pattern: /[0-9]/ }; | |
c.mask("AB/CD/0000", d); | |
} | |
}).keyup(); |
I fixed the bug of caueSantos in cursor, just add 3 lines on the same code
$('[data-format="date"]')
.mask('AB/CD/0000', {
translation: {
A: { pattern: /[0-3]/ },
B: { pattern: /[0-9]/ },
C: { pattern: /[0-1]/ },
D: { pattern: /[0-9]/ }
},
onKeyPress: function(a, b, c, d) {
if (!a) return;
let m = a.match(/(\d{1})/g);
if (!m) return;
if (parseInt(m[0]) === 3) {
d.translation.B.pattern = /[0-1]/;
} else {
d.translation.B.pattern = /[0-9]/;
}
if (parseInt(m[2]) == 1) {
d.translation.D.pattern = /[0-2]/;
} else {
d.translation.D.pattern = /[0-9]/;
}
let temp_value = c.val();
c.val('');
c.unmask().mask('AB/CD/0000', d);
c.val(temp_value);
}
})
.keyup();
Thank you.
Hi fellows, can you show how to validate phone numbers?
You can use for credit card expiry date
if (typeof $.fn.mask === "function") {
$('[data-format="card-expiry-date"]')
.mask("AB/CD", {
translation: {
A: { pattern: /[0-9]/ },
B: { pattern: /[0-9]/ },
C: { pattern: /[2-9]/ },
D: { pattern: /[0-9]/ }
},
onKeyPress: function(a, b, c, d) {
if (!a) return;
let m = a.match(/(\d{1})/g);
if (!m) return;
if (parseInt(m[0]) === 0) {
d.translation.B.pattern = /[1-9]/;
} else if (parseInt(m[0]) === 1) {
d.translation.B.pattern = /[0-2]/;
} else if (parseInt(m[0]) > 1) {
c.val("0" + m[0] + "/");
} else {
d.translation.B.pattern = /[0-9]/;
}
let temp_value = c.val();
c.val("");
c.unmask().mask("AB/CD", d);
c.val(temp_value);
}
})
.keyup();
}
For example:
2/23 => 02/23
Min value: 01/20
Max value: 12/99
@benfiratkaya thanks 😄
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A little fix!
I think it's not the best approach, but for a while seems useful!