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

时间:2015-03-10 07:23:58

标签: javascript jquery

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

1 个答案:

答案 0 :(得分:0)

尝试以下

<强> 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

 });
});

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