jQuery限制文本框输入,包含粘贴。
//限制文本框的输入长度 $(function () { $(document).on("keypress", ".txt-valid-len", function (e) { if (e.keyCode == 8) { return true; } else { var len = $(this).data("maxlength") || 0; if (len > 0) { return (this.value.length <= len); } } return true; }); //粘贴 $(document).on("paste", ".txt-valid-len", function () { var len = $(this).data("maxlength") || 0; if (len > 0) { return ((this.value.length + event.clipboardData.getData('Text').length) <= len); } return true; }); $(document).on("keyup input", ".txt-valid-len", function (e) { var keyCode = e.keyCode || e.which || e.charCode; if (keyCode == 46 || keyCode == 8) { } else { var len = $(this).data("maxlength") || 0; if (len > 0) { if (this.value.length > len) { this.value = com.cutStr(this.value, len, ""); } } } }); });
里面有用到:http://www.cnblogs.com/wz122889488/p/6273018.html里面的方法