Disable copy paste of alphabets in text field using jquery

后端 未结 6 1740
遇见更好的自我
遇见更好的自我 2021-01-14 05:33

In my project i have text field that only take numeric value.but when I copy an alphabets using ctl+c and paste using ctl+v it will allow the alphabets in the text field.So

6条回答
  •  说谎
    说谎 (楼主)
    2021-01-14 05:54

    This functions is for jQuery. It permits only numbers to textboxes The Code:

    jQuery.fn.filter = function() {
        $(this).keydown(function (e) {
            if (e.shiftKey || e.ctrlKey || e.altKey) { // if shift, ctrl or alt keys held down
                e.preventDefault();         // Prevent character input
            } else {
                var n = e.keyCode;
                if (!((n == 8)              // backspace
                || (n == 46)                // delete
                || (n >= 35 && n <= 40)     // arrow keys/home/end
                || (n >= 48 && n <= 57)     // numbers on keyboard
                || (n >= 96 && n <= 105))   // number on keypad
                ) {
                    e.preventDefault();     // Prevent character input
                }
            }
      $(document).mousedown(function(e){ 
        if( e.button == 2 ) { 
          e.preventDefault();         // Prevent character input
            } else {
                var n = e.keyCode;
                if (!((n == 8)              // backspace
                || (n == 46)                // delete
                || (n >= 35 && n <= 40)     // arrow keys/home/end
                || (n >= 48 && n <= 57)     // numbers on keyboard
                || (n >= 96 && n <= 105))   // number on keypad
                ) {
                    e.preventDefault();    
                }
        }
        });
    }
    

    Im nt sure about your proper need but try this. sry if its nt wat u want.

提交回复
热议问题