How to set jquery mask with range

500 Views Asked by At

I have a text box which I have masked like

  $.mask.definitions['x'] = "[0-9]";
  $(".address").mask("xxx:xxx:xxx:xxx");

Here I always want to add range as well i.e xxx < 256. What will be the best way to do this TIA

Unable to edit this textbox

2

There are 2 best solutions below

0
Sachin Tripathi On
$(".address").inputmask("numeric", {
  min: 0,
  max: 256
});
3
Yeongjun Kim On

demo: https://jsbin.com/pohehil

var masking = function(value) {
  if (value.match(/25[0-9]{0,1}/)) {
    return "255"
  } else if (value.match(/2[0-9]{0,2}/)) {
    return "250"
  } else {
    return "200"
  }
}

var options = {
  onKeyPress: function(cep, e, field, options) {
    var mask = ["200", "200", "200", "200"];

    var values = cep.split(":");

    for (var i = 0; i < values.length; i++) {
      mask[i] = masking(values[i]);
    }

    $(e.target).mask(mask.join(":"), options);
  }, 
  translation: {
    2: { pattern: /[0-2]/ },
    5: { pattern: /[0-5]/ }
  }
};

$(".address").mask("200:200:200:200", options);