jQuery文本框过滤插件filtertext

本文介绍了一个可扩展的文本框过滤插件,包括数字、字母、特殊字符、邮箱、汉字、数字字母组合、数字点组合等过滤功能,通过自定义正则表达式实现输入限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
* @Title: jquery.alvin.filtertext.js
* @Description: 文本框过滤插件(可扩展),Example:$("input").filterNumber();
* @author Alvin.zengqi
* @date 2011-6-4 下午12:07:33
* @version V1.0
* @Company: MSD.
* @Copyright Copyright (c) 2011
*/
(function($) {

$.extend({

filterText : new function(){
this.defaults = {
exp : /[]/ig //默认表达式
};
this.construct = function(settings){
return this.each(function(){
var config;
config = $.extend($.filterText.defaults, settings);
$(this).bind("keyup",function(){$.filter(this, config);})
.bind("keydown",function(){$.filter(this, config);});
});
};
},

//只能输数字(包含换行,空格)
filterNumber : new function(){
this.defaults = {
exp : /[^\d\r\n]/ig //默认表达式
};
this.construct = function(settings){
return this.each(function(){
var config;
config = $.extend($.filterNumber.defaults, settings);
$(this).bind("keyup",function(){$.filter(this, config);})
.bind("keydown",function(){$.filter(this, config);});
});
};


},

//只能输字母(包含换行,空格)
filterLetter : new function(){
this.defaults = {
exp : /[^\w]/ig //默认表达式
};
this.construct = function(settings){
return this.each(function(){
var config;
config = $.extend($.filterLetter.defaults, settings);
$(this).bind("keyup",function(){$.filter(this, config);})
.bind("keydown",function(){$.filter(this, config);});
});
};
},


//过滤特殊字符(包含换行,空格)
filterSpecialCharacter : new function(){
this.defaults = {
exp : /[^\u4E00-\u9EA5\w\d\r\n]/ig //默认表达式
};
this.construct = function(settings){
return this.each(function(){
var config;
config = $.extend($.filterSpecialCharacter.defaults, settings);
$(this).bind("keyup",function(){$.filter(this, config);})
.bind("keydown",function(){$.filter(this, config);});
});
};
},

//过滤邮箱(包含换行,空格)
filterEmail : new function(){
this.defaults = {
exp : /[^\d\w\.\@\r\n]/ig //默认表达式
};
this.construct = function(settings){
return this.each(function(){
var config;
config = $.extend($.filterEmail.defaults, settings);
$(this).bind("keyup",function(){$.filter(this, config);})
.bind("keydown",function(){$.filter(this, config);});
});
};
},

//只能输汉字(包含空格)
filterCN : new function(){
this.defaults = {
exp : /[^\u4E00-\u9FA5\uF900-\uFA2D\r\n]/ig //默认表达式
};
this.construct = function(settings){
return this.each(function(){
var config;
config = $.extend($.filterCN.defaults, settings);
$(this).bind("keyup",function(){$.filter(this, config);})
.bind("keydown",function(){$.filter(this, config);});
});
};
},

//只能输数字跟字母(包含换行,空格)
filterNumberLetter : new function(){
this.defaults = {
exp : /[^\d\w\r\n]/ig //默认表达式
};
this.construct = function(settings){
return this.each(function(){
var config;
config = $.extend($.filterNumberLetter.defaults, settings);
$(this).bind("keyup",function(){$.filter(this, config);})
.bind("keydown",function(){$.filter(this, config);});
});
};
},

//只能输数字和.(包含换行,空格) IP
filterNumberPoint : new function(){
this.defaults = {
exp : /[^\d\r\n\.\;]/ig //默认表达式
};
this.construct = function(settings){
return this.each(function(){
var config;
config = $.extend($.filterNumberPoint.defaults, settings);
$(this).bind("keyup",function(){$.filter(this, config);})
.bind("keydown",function(){$.filter(this, config);});
});
};
},

//过滤电话号码
filterPhone : new function(){
this.defaults = {
exp : /[^\d\r\n\-]/ig //默认表达式
};
this.construct = function(settings){
return this.each(function(){
var config;
config = $.extend($.filterPhone.defaults, settings);
$(this).bind("keyup",function(){$.filter(this, config);})
.bind("keydown",function(){$.filter(this, config);});
});
};
},

filter : function(obj, config){
if(config.exp.test(obj.value))obj.value=obj.value.replace(config.exp,'');
}

});

$.fn.extend({
//过滤文本(自定义)
filterText : $.filterText.construct,
//只能输数字(包含换行,空格)
filterNumber : $.filterNumber.construct,
//只能输字母(包含换行,空格)
filterLetter : $.filterLetter.construct,
//过滤特殊字符(包含换行,空格)
filterSpecialCharacter : $.filterSpecialCharacter.construct,
//过滤邮箱(包含换行,空格)
filterEmail : $.filterEmail.construct,
//只能输汉字(包含空格)
filterCN : $.filterCN.construct,
//只能输数字跟字母(包含换行,空格)
filterNumberLetter : $.filterNumberLetter.construct,
//只能输数字和.(包含换行,空格) IP
filterNumberPoint : $.filterNumberPoint.construct,
//过滤电话号码
filterPhone : $.filterPhone.construct
});

})(jQuery);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值