jQuery Mask Plugin - sample
by Juan Muñoz
HTML
<script src="http://igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js"></script>
<div>
<h2>From 'Basic Usage Examples'</h2>
<div class="col">
<label>Date (off topic, just for fun)</label>
<input type="text" class="fecha" />
</div>
</div>
CSS
label{
font-weight: bold;
}
JavaScript
// As in docs (http://igorescobar.github.io/jQuery-Mask-Plugin/)
// In case you're wondering, dates are pt_BR ;-)
$(document).ready(function () {
// 'Basic Usage Examples'
$('.fecha').mask(
'AB-CD-EFGH', {
translation: {
'A': { pattern: /[0-3]/ },
'B': { pattern: /[0-9]/ }, // <- We need to fix this guy here
'C': { pattern: /[0-1]/ },
'D': { pattern: /[0-9]/ },
'E': { pattern: /[0-9]/ },
'F': { pattern: /[0-9]/ },
'G': { pattern: /[0-9]/ },
'H': { pattern: /[0-9]/ }
}
}).keyup(function(evt){
let valor = $(this).val();
let largo = $(this).val().length;
let tecla = evt.keyCode;
if ( largo == 2){
let digito_uno = valor.slice(0,1);
if( digito_uno == 3 ){
let digito_dos = valor.slice(1,2);
if (digito_dos > 1) {
$('.fecha').val(digito_uno)
}
}
}else if (largo == 5){
let valor = $(this).val();
let anterior = valor.slice(0,4);
let digito_cuatro = valor.slice(3,4);
let digito_cinco = valor.slice(4,5);
if (digito_cuatro == 1) {
if (digito_cinco > 2) {
$('.fecha').val(anterior)
}
}
}
}).blur(function (){
let largo = $(this).val().length;
let valor = $(this).val();
if ( largo == 10)
console.log("es"+validaFechaCompra(valor) );
});;
function validaFechaCompra(fecha) {
var dtCh = "-";
var minYear = 1900;
var maxYear = 2100;
function isInteger(s) {
var i;
for (i = 0; i < s.length; i++) {
var c = s.charAt(i);
if (((c < "0") || (c > "9")))
return false;
}
return true;
}
function stripCharsInBag(s, bag) {
var i;
var returnString =...