//
带有小数的数字掩码
var NumberMaskConfig = ... {
defaultTextAlign : "center",
defaultAutoNull : true,
defaultFormatString : "##.dd",
defaultHiddenFormatString : "##.dd",
// 可用的功能键值数组.
WORK_KEY : [8, 33, 34, 35, 36, 37, 38, 39, 40, 46, 144],
// 数字键值数组.
NUMBER_KEY : [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105]
} ;
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 =
var NumberMaskConfig = ... {
defaultTextAlign : "center",
defaultAutoNull : true,
defaultFormatString : "##.dd",
defaultHiddenFormatString : "##.dd",
// 可用的功能键值数组.
WORK_KEY : [8, 33, 34, 35, 36, 37, 38, 39, 40, 46, 144],
// 数字键值数组.
NUMBER_KEY : [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105]
} ;
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 =