EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: galcott on June 23, 2021, 10:46:08 AM



Title: Filter function on numberbox
Post by: galcott on June 23, 2021, 10:46:08 AM
When using numberboxes, sometimes I want to allow a decimal point and other times not to allow it. I can't figure out how to do this. I see there is a filter property that takes a function, but absolutely no information on how to use it. Can you provide an example of a filter function, or another way to specify whether to allow a decimal? This is missing from the documentation. Thank you.


Title: Re: Filter function on numberbox
Post by: jarry on June 25, 2021, 02:34:20 AM
The 'precision' property can be defined to specify the precision to display after the decimal separator. If you doesn't allow it, set the 'precision' to 0.

Code:
$('#nb').numberbox({precision:2})


Title: Re: Filter function on numberbox
Post by: galcott on June 25, 2021, 06:27:19 AM
Even if you set precision to 0, it still allows the decimal point. I don't want to allow it at all. I'm using it to enter fields like Zip code and credit card number.


Title: Re: Filter function on numberbox
Post by: jarry on June 25, 2021, 07:12:19 AM
The decimal point will be removed and the value will be fixed after the user press ENTER key or the inputing box loses focus. If you want to disable the decimal point absolutely, custom the 'filter' function to prevent from entering it.
Code:
$('#nb').numberbox({
precision: 0,
filter: function(e){
if (String.fromCharCode(e.which) == '.'){
return false;
} else {
return $.fn.numberbox.defaults.filter.call(this, e);
}
}
})


Title: Re: Filter function on numberbox
Post by: galcott on June 25, 2021, 07:28:19 AM
This is what I was looking for. That sample code should be in the documentation, since it doesn't currently include any explanation of how to use the filter function.