HTML text input allow only numeric input

前端 未结 30 3508
孤街浪徒
孤街浪徒 2020-11-21 04:58

Is there a quick way to set an HTML text input () to only allow numeric keystrokes (plus \'.\')?

30条回答
  •  难免孤独
    2020-11-21 05:28

    Please find below mentioned solution. In this user can be able to enter only numeric value, Also user can not be able to copy, paste, drag and drop in input.

    Allowed Characters

    0,1,2,3,4,5,6,7,8,9
    

    Not allowed Characters and Characters through events

    • Alphabetic value
    • Special characters
    • Copy
    • Paste
    • Drag
    • Drop

    $(document).ready(function() {
      $('#number').bind("cut copy paste drag drop", function(e) {
          e.preventDefault();
      });     
    });
    function isNumberKey(evt) {
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
        return true;
    }
    
    
    
    

    Let me know if it not works.

提交回复
热议问题