html输入框只允许输入,JavaScript实现input输入框控件只允许输入数字

(function ($) {

var my_validate_plug_name = "myvalidate";

function MyJqValidate(element, options){

this.init(element, options);

}

MyJqValidate.prototype = {

init: function (element, options) {

var allowFilter = ["positiveNumber"];

var defaults = {filter_type: "positiveNumber", enterCallback: function (obj){},

valCallback: function (val){}};

this.element = element;

this.settings = $.extend( {}, defaults, options );

if($.inArray( this.settings.filter_type, allowFilter) == -1) {

return;

}

this[this.settings.filter_type].call(this);

},

positiveNumber: function (){

var _this = this;

/* 大于0的正则匹配 匹配:0.1 1. 3.0 .1 */

this.element.keyup(function (e){

var code = e.keyCode || e.which;

var txt = $(this).val();

var reg = /^(0*\.?[1-9]\d*|[1-9]+(\d*\.\d*)?)$/;

if(reg.test(txt)){

callback.call(_this, e);

return;

}

var numb = txt.match(/\d|\./g);

if(numb == null) {

$(this).val("");

callback.call(_this, e);

return;

}

numb = numb.join("");

var str = "";

var f = true;

for(var i=0; i

var s = numb[i];

if (s=="."){

if (f) {

str += s;

f = false;

}

} else {

str += s;

}

}

$(this).val(str);

callback.call(_this, e);

});

function callback(e){

this.value = $.trim(this.element.val());

var v = parseFloat(this.value);

this.format_value = isNaN(v) ? 0 : v;

this.settings.valCallback(this.format_value);

var code = e.keyCode || e.which;

if(code == 13) { //Enter keycode

this.settings.enterCallback(this.element);

}

}

}

}

$.fn[my_validate_plug_name] = function(options){

var elt;

if ( options instanceof Object || !this.data( "plugin_" + my_validate_plug_name ) ) {

elt = new MyJqValidate( this, options );

this.data('plugin_' + my_validate_plug_name, elt);

} else {

elt = this.data( "plugin_" + my_dialog_plug_name );

}

if (typeof(options) == "string" && options.length>0){

eval("elt."+options+"(this)");

}

return this;

}

}( jQuery ));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值