jQuery限制文本框输入,包含粘贴。
代码如下 | 复制代码 |
//限制文本框的输入长度 $(function() { $(document).on("keypress",".txt-valid-len",function(e) { if(e.keyCode == 8) { returntrue; } else{ varlen = $(this).data("maxlength") || 0; if(len > 0) { return(this.value.length <= len); } } returntrue; }); //粘贴 $(document).on("paste",".txt-valid-len",function() { varlen = $(this).data("maxlength") || 0; if(len > 0) { return((this.value.length + event.clipboardData.getData('Text').length) <= len); } returntrue; }); $(document).on("keyup input",".txt-valid-len",function(e) { varkeyCode = e.keyCode || e.which || e.charCode; if(keyCode == 46 || keyCode == 8) { } else{ varlen = $(this).data("maxlength") || 0; if(len > 0) { if(this.value.length > len) { this.value = com.cutStr(this.value, len,""); } } } }); }); |