KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

TextBox limit user input to numbers only or whole numbers or numbers with decimals #2815

Closed cfsinc opened this topic on on Sep 1, 2022 - 7 comments

cfsinc commented on Sep 1, 2022

How do I limit the user input of textbox to numbers or numbers with decimals only. Not allow any other characters?

KoolReport commented on Sep 2, 2022

Sorry for my late reply, we are in national holiday. To do this you need to register the keypress event and allow only number to be passed. Something like this:

TextBox::create(array(
    ...
    "clientEvents"=>array(
        "keypress"=>"function(event) {
           $(this).val($(this).val().replace(/[^\d].+/, ''));
            if ((event. Which < 48 || event. Which > 57)) {
                event.preventDefault();
            }
        }"
    )
    ...
))

Note: My code is just to represent the idea

Hope that helps.

cfsinc commented on Sep 2, 2022

ok thank you for the reply. I will use your code as a starting point. I really appreciate the reply during a holiday.

KoolReport commented on Sep 3, 2022

May be you try this code for decimals number:

"function(event){
     $(this).val($(this).val().replace(/[^0-9\.]/g,''));
            if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
                event.preventDefault();
}"
cfsinc commented on Sep 4, 2022

Just trying to implement your code and go from there im getting a syntaxError that I cant seem to get past. I am not understanding why this syntax error is happening? it does not like the clientEvent array???

cfsinc commented on Sep 4, 2022

Sorry im dumb. I see the missing } now

cfsinc commented on Sep 4, 2022

Your code seems to work really great!!!! Thank you so much!!! I hope you guys have a great holiday!!!!

KoolReport commented on Sep 4, 2022

That's great :D

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed
solved

Inputs