jQuery实现限制文本框的输入长度
程序员文章站
2023-11-23 15:16:34
jquery限制文本框输入,包含粘贴。
//限制文本框的输入长度
$(function () {
$(document).on("keypress", "....
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, ""); } } } }); });
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!