EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: Darrel on September 28, 2016, 11:08:38 PM



Title: Restricting the number of integers in a numberbox
Post by: Darrel on September 28, 2016, 11:08:38 PM
How do i restrict the no. of digits that can be entered by the user in a numberbox?


Title: Re: Restricting the number of integers in a numberbox
Post by: jarry on September 29, 2016, 02:18:27 AM
The 'filter' function defines how to filter the key pressed, return true to accept the inputed char.
Code:
$('#n1').numberbox({
filter: function(e){
var opts = $(this).numberbox('options');
var s = $(this).numberbox('getText');
if (e.metaKey || e.ctrlKey){
return true;
}
if ($.inArray(String(e.which), ['46','8','13','0']) >= 0){ // DELETE BACKSPACE ENTER
return true;
}
var tmp = $('<span></span>');
tmp.html(String.fromCharCode(e.which));
var c = tmp.text();
tmp.remove();
if (!c){
return true;
}
if (c == '-' || c == opts.decimalSeparator){
return (s.indexOf(c) == -1) ? true : false;
} else if (c == opts.groupSeparator){
return true;
} else if ('0123456789'.indexOf(c) >= 0){
return true;
} else {
return false;
}
}
})