js如何限制TextBox只能输入某范围的整数
函数定义:
function InputNumb(MaxValue,edit)
{
if(...)
return;
.......
window.event.returnValue = false;
return;
}
以上需求能否用正则表达式实现?
[解决办法]
算了,还是给个现成的你吧//很久以前写的.差不多能用.//iDeciLength decimal lengthfunction CheckNumberInput(iLength,iDeciLength,iAllowZeroCapital){event.cancelBubble =true; var sClipBoardText = window.clipboardData.getData( "Text ");if (sClipBoardText==null )sClipBoardText = " ";if(event.ctrlKey ){if(event.keyCode == 67 ||event.keyCode ==99||event.keyCode ==10 || event.keyCode ==13) // Ctrl + C{return true;}if(!isNaN(sClipBoardText) && sClipBoardText.length <=iLength){return true;}return false;}if (iDeciLength> 0 && iLength <3 ) return false; if(event.keyCode == 9 ) return true;var c = String.fromCharCode(event.keyCode);var keyCodeAry = new Array(96,97,98,99,100,101,102,103,104,105)var keyCode = event.keyCode;var specialKeyAry = new Array(13,27,8,46,35,36,37,38,39,40);if (itemInAry(specialKeyAry,keyCode)) return true;var text= event.srcElement.value;if(document.selection.createRange().text==text && event.keyCode !=9){event.srcElement.value = " ";text = " ";event.srcElement.focus();}//get text cursor positionvar obj = window.event.srcElement;var s=document.selection.createRange();s.setEndPoint( "StartToStart ",obj.createTextRange())var pos = s.text.length;if((isNaN(c)||keyCode==32 ||event.shiftKey) && !itemInAry(keyCodeAry,keyCode)){if ( text.indexOf( ". ",0)==-1 &&(iLength> 0) && (keyCode ==110||(keyCode==190 && !event.shiftKey)) ) {if(pos == text.length && (iLength-text.length) <2 && document.selection.createRange().text!=text){return false;}else{} }else return false;if (iDeciLength==0) return false;}if(text.length==iLength && !(document.selection.createRange().text==text) ) return false; //max length and not hightlightif(pos ==0 && (keyCode==110||(keyCode==190 && !event.shiftKey)) && text.length <iLength-1){obj.value = "0. ";return false;;}if(pos ==0 && iDeciLength> 0 && text== " " && (c== "0 "||keyCode==96)){obj.value = "0. ";return false;}if( (c== "0 "||keyCode==96) && iDeciLength <=0 &&pos==0 && iAllowZeroCapital!=1){return false;}var zeroPos = text.indexOf( ". ");if(zeroPos> 0 && pos > zeroPos){if (text.length-zeroPos-1 ==iDeciLength) return false;}//alert(pos.length);return true;}function itemInAry(ary,item){for (var i =0;i <ary.length;i++){if (ary[i]==item){return true;}}return false;}//Add End