//////////////
/*
*只允许输入数字值(整数)*调用方法: OnKeyPress="javascript:return OnlyInt();"
**/
function OnlyInt(){
if (event.keyCode == 13)
return true
if (event.keyCode < 48 || event.keyCode > 57){
alert( "只能输入数字!" );
return false;
}else
return true;
}
/*
*只允许输入数字值(整数)*调用方法: OnKeyPress="javascript:return OnlyInt();"
**/
function OnlyInt(){
if (event.keyCode == 13)
return true
if (event.keyCode < 48 || event.keyCode > 57){
alert( "只能输入数字!" );
return false;
}else
return true;
}
to gzty(风逍遥) ( ) :你的也不行啊在网上搜到这个:
<script>
function regInput(obj, reg, inputStr)
{
var docSel = document.selection.createRange()
if (docSel.parentElement().tagName != "INPUT") return false
oSel = docSel.duplicate()
oSel.text = ""
var srcRange = obj.createTextRange()
oSel.setEndPoint("StartToStart", srcRange)
var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length)
return reg.test(str)
}
</script>
<BODY>
<input onkeypress="return regInput(this,/^[0-9]*$/,String.fromCharCode(event.keyCode))"
onpaste="return regInput(this,/^[0-9]*$/,window.clipboardData.getData('Text'))"
ondrop="return regInput(this,/^[0-9]*$/,event.dataTransfer.getData('Text'))">
但是对于中文输入法的情况依然没办法,中文还是照样输入。
{
obj.value = obj.value.replace(/([\d]*)(\D*)/g,"$1") ;
}
<input id="Text1" type="text" onchange="javascript:checkInput(this);"/>
function regInput(obj, reg, inputStr)
{
var docSel = document.selection.createRange()
if (docSel.parentElement().tagName != "INPUT") return false
oSel = docSel.duplicate()
oSel.text = ""
var srcRange = obj.createTextRange()
oSel.setEndPoint("StartToStart", srcRange)
var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length)
return reg.test(str)
}
</script>
<BODY>
<input onkeypress="return regInput(this,/^[0-9]*$/,String.fromCharCode(event.keyCode))"
onpaste="return regInput(this,/^[0-9]*$/,window.clipboardData.getData('Text'))"
ondrop="return regInput(this,/^[0-9]*$/,event.dataTransfer.getData('Text'))">这个可以解决复制之类的问题,但是对待中文输入还是没法
if ( a = _onlyNum.arguments[vii] ) {
if ( o = document.getElementById(a) ) {
with (o) {
style.imeMode = "disabled"
onchange = function (){
value = value.replace(/\D/g,'')
}
onkeypress = function (){
return (/[\d]/.test(String.fromCharCode(event.keyCode)))
}
ondrop = function (){
return /\D/g.test(event.dataTransfer.getData("text")) ? false : true
}
onbeforepaste = function (){
onpaste = function (){return /\D/g.test(clipboardData.getData('text')) ? false : true}
}
}
}
}
}
}
</script><body onload="_onlyNum('tt','tt1')">
<input name="tt"> <input name="tt1">
</body></html>
<script>
function checkInput(obj)
{
obj.value = obj.value.replace(/([\d]*)(\D*)/g,"$1") ;
}
</script>
<input id="Text1" type="text" onchange="javascript:checkInput(this);"/>
<script language="javascript">
function disable(){
document.myform.text1.style.imeMode="disabled";
}
</script>