How to prevent user from entering decimals?

后端 未结 3 1060
北恋
北恋 2020-12-19 04:02

I\'ve got an order page on my site. Some order items can have decimal quantities, some can\'t.

What\'s the best way to prevent the user from entering decimal quanti

3条回答
  •  别那么骄傲
    2020-12-19 04:30

    You can add an event (on key press) and detect if a decimal point has been pressed by the user or not. Also on form submission, use regular expressions to check if the submitted data is correct (because the user can forge the data using live editor like firebug). Also make sure to double check that on your server side in case if user disabled javascript.

    for example:

    
    
    
    function checkDecimal() {
        // your code goes here
    }
    
    function checkBeforeSubmit() {
        // your code goes here
    }
    

    You better to use the same function cause it's basically the same thing and invoke it from both events.

    On server side, check the submitted data again

提交回复
热议问题