jQuery实现限制文本框的输入长度

时间:2017-01-11    点击:109   

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, "");
 }
 }
 }
 });
});

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持本站!

实现一个简单的vue无限加载指令方法
使用vue.js实现联动效果的示例代码
jQuery zTree树插件简单使用教程
详解JS中的快速排序与冒泡
VueJs路由跳转——vue-router的使用详解
> 返回     
地址:上海市普陀区胶州路941号长久商务中心 电话: QQ:
© Copyright 2012 上海网络 Product All Rights Reserved