I'm struggling with this for a day now, probably is really easy to solve.
What I want is the search field to only accept numbers, so I tried something like this:
function onlyNumbers(){
$('.select2-search__field').on('keypress', function () {
$(this).val($(this).val().replace(/[^\d].+/, ""));
if ((event.which < 48 || event.which > 57)) {
event.preventDefault();
}
});
}
function select2(){
$(".select2From").select2({
tags: true,
placeholder: "de",
allowClear: true
});
$(".select2To").select2({
tags: true,
placeholder: "até",
allowClear: true
});
}
But that doesn't work...actually I can't even get to the '.select2-search__field'
. Doesn't matter if is on keypress, on change, on click, on something...I just want to know how can I get to that input so I can filter the text on input.
Hope you can help me. Thank you!