javascript::::::
function numtest(str)
{
var checkOK = "0123456789";
var checkStr = str;
var ifValid = true;
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
ifValid = false;
break;
}
}
return ifValid;
}
function numtest(str)
{
var checkOK = "0123456789";
var checkStr = str;
var ifValid = true;
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
ifValid = false;
break;
}
}
return ifValid;
}
alert("\请输入数字");
return false;
}
QueryTermAll.QueryTerm.value是你的文本框的值
多谢,我已经找到一个比较简单的方法了,show it to u,当然你的答案也不错,只是晚了点...
public class enterint implements KeyListener{
public void keyTyped(KeyEvent e){
try{
if(e.getKeyChar()!='0'&e.getKeyChar()!='1'&e.getKeyChar()!='2'&e.getKeyChar()!='3'&e.getKeyChar()!='4'&e.getKeyChar()!='5'&e.getKeyChar()!='6'&e.getKeyChar()!='7'&e.getKeyChar()!='8'&e.getKeyChar()!='9')
{
e.consume();
}
}catch(Exception ex){
System.err.println(ex);
}
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
用isNaN(value),其中value似乎必须是float/double型,那不是已经是数字了么?