var OnPasteCheckIntNum = function () {//粘贴时验证整数 if (window.clipboardData) { if (isNaN(window.clipboardData.getData('text'))) { clipboardData.setData('text', ''); } else { //clipboardData.setData('text',clipboardData.getData('text')) clipboardData.setData('text', clipboardData.getData('text').replace(/[^/d]/g, '')) } } else if (navigator.userAgent.indexOf("Opera") != -1) { if (isNaN(window.location)) { //window.location = window.location.replace(/[^/d]/g,''); window.location = ''; } } }; var OnPasteCheckDecimal = function (id) {//粘贴时验证带小数的数字 return function () { if (window.clipboardData) { if (isNaN(window.clipboardData.getData('text') + id.value)) { clipboardData.setData('text', ''); } else { clipboardData.setData('text', clipboardData.getData('text')) } } else if (navigator.userAgent.indexOf("Opera") != -1) { if (isNaN(window.location)) { //window.location = window.location.replace(/[^/d]/g,''); window.location = ''; } } } }; var OnBlurNum = function (obj) {//文本框失去焦点时格式化数字如"0001" --> "1" return function () { for (i = 0; i < obj.value.length; i++) { if (obj.value.indexOf("0") == 0) { obj.value = obj.value.substring(1, obj.value.length); } } } }; var CheckIntNum = function (id) {//验证输入是否整数 return function () { var num = id.value.replace(//,/g, ''); if (!((event.keyCode > 47 && event.keyCode < 58))) { event.keyCode = 0; } } }; var CheckDecimal = function (id) {//验证输入是否数字 return function () { var num = id.value.replace(//,/g, ''); if (num.indexOf(".") == -1) { if (!((event.keyCode > 47 && event.keyCode < 58) || (event.keyCode == 46))) { event.keyCode = 0; } } else { if (!(event.keyCode > 47 && event.keyCode < 58)) { event.keyCode = 0; } } } }; //添加事件到Dom var oEventUtil = new Object(); oEventUtil.AddEventHandler = function (oTarget, sEventType, fnHandler) { //IE和FF的兼容性处理 //如果是FF if (oTarget.addEventListener) { oTarget.addEventListener(sEventType, fnHandler, false); } //如果是IE else if (oTarget.attachEvent) { oTarget.attachEvent('on' + sEventType, fnHandler); } else { oTarget['on' + sEventType] = fnHandler; } }; function SetCheckDecimal(oTarget) { oTarget.style.imeMode = "disabled";//限制输入法切换 oEventUtil.AddEventHandler(oTarget, "keypress", CheckDecimal(oTarget)); oEventUtil.AddEventHandler(oTarget, "blur", OnBlurNum(oTarget)); oEventUtil.AddEventHandler(oTarget, "beforepaste", OnPasteCheckDecimal(oTarget)); } function SetCheckIntNum(oTarget) { oTarget.style.imeMode = "disabled"; //限制输入法切换 oEventUtil.AddEventHandler(oTarget, "keypress", CheckIntNum(oTarget)); oEventUtil.AddEventHandler(oTarget, "blur", OnBlurNum(oTarget)); oEventUtil.AddEventHandler(oTarget, "beforepaste", OnPasteCheckIntNum); } //用法: //<input type="text" id="testEle" name="testEle" /> //SetCheckDecimal(testEle);//限制只能输入数字(带小数点) //SetCheckIntNum(testEle);//限制只能输入整数(不带小数点)
最近用jQuery重构此方法,同时支持FF、Chrome等浏览器,代码如下:/****************************** $("#txtName").validateNumber({type:Int|Decimal,precision:2,align:"right"}); ******************************/ (function($) { $.fn.validateNumber = function(settings) { var defaults = {type:"Int",precision:-1,align:"right"} if (settings){ $.extend(defaults, settings) }; var OnPasteCheckInteger=function(){//粘贴时验证整数 if (window.clipboardData) { if(window.clipboardData.getData('text')) { if (isNaN(window.clipboardData.getData('text')) || window.clipboardData.getData('text').indexOf(".") >= 0) { clipboardData.setData('text', ''); } else { clipboardData.setData('text',clipboardData.getData('text')) } } } else if (navigator.userAgent.indexOf("Opera") != -1) { if(window.location){ if (isNaN(window.location) || window.location.indexOf(".") >= 0) { window.location = ''; } } } }; var OnPasteCheckDecimal = function() {//粘贴时验证带小数的数字 if (window.clipboardData) { if (isNaN(window.clipboardData.getData('text') + this.value)) { clipboardData.setData('text', ''); } else { clipboardData.setData('text', clipboardData.getData('text')) } } else if (navigator.userAgent.indexOf("Opera") != -1) { if (isNaN(window.location + this.value)) { window.location = ''; } } }; var OnBlur = function () {//文本框失去焦点时格式化数字如"0001" --> "1" if(isNaN(this.value)){//如果不是数字则清空 this.value = ""; return; } while(this.value.indexOf("0") == 0 && this.value.length > 1){ this.value = this.value.substring(1, this.value.length); } if(defaults.type == "Decimal"){ if(this.value.indexOf(".") == 0){ this.value = "0"+this.value; } if(defaults.precision >= 0 && this.value.indexOf(".") >= 0){ if(defaults.precision > 0){ this.value = this.value.substr(0,this.value.indexOf(".") + defaults.precision + 1); } else{ this.value = this.value.substr(0,this.value.indexOf(".")); } } } else if(this.value.indexOf(".") >=0){ this.value = this.value.substr(0,this.value.indexOf(".")); } }; var InputCheckInteger = function(e) {//验证输入是否整数 //在FF下屏蔽退格、方向键等功能键,charCode在IE下为undefined非0 if (e.charCode == 0){ return; } var event = window.event || e; var currentKey = event.charCode || event.keyCode || event.which; if (!((currentKey > 47 && currentKey < 58))) { if (window.event){//IE event.returnValue = false; } else{//Firefox event.preventDefault(); } } }; var InputCheckDecimal = function(e) {//验证输入是否数字 //在FF下屏蔽退格、方向键等功能键,charCode在IE下为undefined非0 if (e.charCode == 0){ return; } var event = window.event || e; var currentKey = event.charCode || event.keyCode || event.which; var num = this.value.replace(//,/g, ''); if (num.indexOf(".") == -1) { if (!((currentKey > 47 && currentKey < 58) || (currentKey == 46))) { if (window.event){//IE event.returnValue = false; } else{//Firefox event.preventDefault(); } } } else { if (!(currentKey > 47 && currentKey < 58)) { if (window.event){//IE event.returnValue = false; } else{//Firefox event.preventDefault(); } } } }; this.each(function(){ if($(this).is("input[type=text]") || $(this).is("textarea")){ $(this).css("imeMode","disabled");//限制输入法切换 $(this).css("textAlign",defaults.align); $(this).bind("blur",OnBlur); if(defaults.type == "Decimal"){ $(this).bind("beforepaste",OnPasteCheckDecimal); $(this).bind("keypress",InputCheckDecimal); }else{ $(this).bind("beforepaste",OnPasteCheckInteger); $(this).bind("keypress",InputCheckInteger); } } }); return this; }; })(jQuery);