用js写的一个形式为##.dd的动态小数掩码问题,可以处理负值 NumberMask

这是一个使用JavaScript编写的NumberMask函数,用于处理形式为##.dd的动态小数掩码,包括负值。函数通过监听键盘事件,实现对输入值的格式化,支持删除、粘贴、剪切等操作,并能处理隐藏的格式化数值。
摘要由CSDN通过智能技术生成
// 带有小数的数字掩码 
var  NumberMaskConfig  =   {
    defaultTextAlign        : 
"center",
    defaultAutoNull            : 
true,
    defaultFormatString        : 
"##.dd",
    defaultHiddenFormatString     : 
"##.dd",
    
// 可用的功能键值数组.
    WORK_KEY            : [8333435363738394046144],
    
// 数字键值数组.
    NUMBER_KEY            : [4849505152535455565796979899100101102103104105]
}
;

function  NumberMask(textId, formatString, isAutoNull, hiddenTextId, hiddenNumberFormatString)  {
    
if ( textId == null ) {
        
return;
    }

    
this.element = textId;
    
this.element.style.textAlign = NumberMaskConfig.defaultTextAlign;
    
this.document = textId.ownerDocument || textId.document;
    
this.window = this.document.defaultView || this.document.parentWindow;

    
this.hiddenElementId = hiddenTextId;
    
if ( isAutoNull == null ) {
        isAutoNull 
= NumberMaskConfig.defaultAutoNull;
    }

    
this.autoNull = isAutoNull;
    
var oThis = this;
    
this._onKeyDown = function (e) {
        
if ( e == null ) {
            e 
= this.window.event;
        }

        oThis.onKeyDown(e);
    }
;
    
this._onBeforePaste = function (e) {
        
if ( e == null ) {
            e 
= this.window.event;
        }

        oThis.onBeforePaste (e);
    }
;
    
this._onBeforeCut = function (e) {
        
if ( e == null ) {
            e 
= this.window.event;
        }

        oThis.onBeforeCut (e);
    }
;
    
this._onContextMenu = function (e) {
        
if ( e == null ) {
            e 
= this.window.event;
        }

        oThis.onContextMenu (e);
    }
;
    
this._onFocus = function (e) {
        
if ( e == null ) {
            e 
= this.window.event;
        }

        oThis.onFocus (e);
    }
;
    
this._onBlur = function (e) {
        
if ( e == null ) {
            e 
= this.window.event;
        }

        oThis.onBlur (e);
    }
;
    
    
this.initMask( formatString || NumberMaskConfig.defaultFormatString );
    
    
if ( this.hiddenElementId != null ) {
        
this.initHiddenMask( hiddenNumberFormatString || NumberMaskConfig.defaultHiddenFormatString );
    }

    
    
this.initEvent();
    
    
this.initValue(this.element.value);
}


// 初始化格式参数.
//
参数:formatString 格式化字符串.
NumberMask.prototype.initMask  =   function  ( formatString )  {
    
// 校验格式字符串,若有误,设置为默认格式.
    if (       ( formatString.indexOf("#">= 0 && formatString.indexOf("#"!= formatString.lastIndexOf("#"- 1 )
        
|| ( formatString.indexOf("d">= 0 && formatString.indexOf("d"!= formatString.lastIndexOf("d"- 1 ) ) {
        
        formatString 
= NumberMaskConfig.defaultFormatString;
    }

    
    
this.maskString = formatString;
    
    
this.integerIndex = formatString.search("##");
    
this.doubleIndex = formatString.search("dd");
}
;

// 初始化隐藏项的格式参数.
//
参数:formatString 格式化字符串.
NumberMask.prototype.initHiddenMask  =   function  ( formatString )  {
    
// 校验格式字符串,若有误,设置为默认格式.
    if (       ( formatString.indexOf("#">= 0 && formatString.indexOf("#"!= formatString.lastIndexOf("#"- 1 )
        
|| ( formatString.indexOf("d">= 0 && formatString.indexOf("d"!= formatString.lastIndexOf("d"- 1 ) ) {
        
        formatString 
= NumberMaskConfig.defaultHiddenFormatString;
    }

    
    
this.hiddenMaskString = formatString;
    
    
this.hiddenintegerIndex = formatString.search("##");
    
this.hiddendoubleIndex = formatString.search("dd");
}
;

// 初始化事件.
NumberMask.prototype.initEvent  =   function  ()  {
    
if ( typeof this.element.addEventListener != "undefined" ) {
        
this.element.addEventListener("keydown"this._onKeyDown, false);
        
this.element.addEventListener("beforepaste"this._onBeforePaste, false);
        
this.element.addEventListener("beforecut"this._onBeforeCut, false);
        
this.element.addEventListener("contextmenu"this._onContextMenu, false);
        
this.element.addEventListener("focus"this._onFocus, false);
        
this.element.addEventListener("blur"this._onBlur, false);
    }
 else if ( typeof this.element.attachEvent != "undefined" )    {
        
this.element.attachEvent("onkeydown"this._onKeyDown);
        
this.element.attachEvent("onbeforepaste"this._onBeforePaste);
        
this.element.attachEvent("onbeforecut"this._onBeforeCut);
        
this.element.attachEvent("oncontextmenu"this._onContextMenu);
        
this.element.attachEvent("onfocus"this._onFocus);
        
this.element.attachEvent("onblur"this._onBlur);
    }

}
;

//  初始化默认值.
NumberMask.prototype.initValue  =   function  (aValue)  {
    
if ( this.autoNull && (aValue.length <= 0 || aValue == null )) {
        
this.element.value = "";
    }
 else {
        
this.element.value = this.formatTheNumber(aValue);
        
if ( this.autoNull && !this.isValidNumber() ) {
            
this.element.value = "";
        }

    }

    
this.setHiddenValue();
}
;

//  判断键入的是不是数字.
//
 参数:pressKeyCode 从键盘输入值.
//
 返回:true 是数字;false 不是数字.
NumberMask.prototype.isNumberOrMinus  =   function  ( pressKeyCode )  {
    
if ( pressKeyCode == null || typeof(pressKeyCode) != "number" ) {
        
return false;
    }

    
if ( NumberMaskConfig.NUMBER_KEY == null || NumberMaskConfig.NUMBER_KEY.length == 0 ) {
        
return false;
    }

    
for ( var i = 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值