限制用户使用按键事件 injQuery 复制和粘贴声明为数字文本框的文本框中的字符

restrict the user to copy and paste characters in the text box which is declared as Numeric text box by using key press event injquery

本文关键字:文本 声明 数字 字符 用户 复制 injQuery 事件      更新时间:2023-09-26

如何使用jquery中的按键事件限制用户在声明为数字文本框的文本框中复制和粘贴字符

请尝试以下操作

.html

<input type="text" ID="TextBox1" runat="server" >

.js

$(document).ready(function () {
  $("#TextBox1").keypress(function (e) {
     //if the letter is not digit then display error and don't type anything
     if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
        //display error message
        $("#errmsg").html("Digits Only").show().fadeOut("slow");
               return false;
    }
   });
});
$(document).ready(function() {
 $('#TextBox1').bind('copy paste cut',function(e) { 
 e.preventDefault(); //disable cut,copy,paste
 });
});

这将禁用复制,粘贴功能主义者,它只允许用户输入数字
演示