先写个JS文件,文件里包含下边这样一段代码:(分别用来验证输入内容是否为整型或浮点型数据)
function vInteger(pNum,pIntegerDigit)
{
if((typeof pIntegerDigit).toLowerCase() =='undefined')
{
if(!RegExpTest(pNum,'^[ ]*[0-9]{0,}[ ]*$') || trim(pNum).length==0)return false;  //Error Integer format
}
else
{
if(!RegExpTest(pNum,'^[ ]*[0-9]{0,'+ pIntegerDigit +'}[ ]*$') || trim(pNum).length==0)return false;  //Error Integer format
}
return true;
}
function vFloat(pNum,pDecimalDigits,pIntegerDigit)
{
if((typeof pDecimalDigits).toLowerCase() =='undefined' && (typeof pIntegerDigit).toLowerCase() =='undefined')
{
if(!RegExpTest(pNum,'^[ ]*[0-9]{0,}(\\.[0-9]{1,})?[ ]*$') || trim(pNum).length==0  || trim(pNum)==".")return false;  //Error float format
}
else if((typeof pIntegerDigit).toLowerCase() =='undefined')
{
if(!RegExpTest(pNum,'^[ ]*([0-9]{0,})(\\.[0-9]{0,'+ pDecimalDigits +'})?[ ]*$') || trim(pNum).length==0 || trim(pNum)=="." )return false;  //Error float format
}
else
{
if(!RegExpTest(pNum,'^[ ]*([0-9]{0,' + pIntegerDigit + '})(\\.[0-9]{0,'+ pDecimalDigits +'})?[ ]*$') || trim(pNum).length==0 || trim(pNum)==".")return false;  //Error float format
}
return true;
}
然后,在你准备验证数据的JSP页面中将该JS文件包含进来。
比如说有个输入框:<input type="text" name="myName" value="" size="30" maxlength="30">
在点了submit按钮后,让页面去调用下边的脚本方法:
function check(){
var checkForm = document.base.myName.value;
if(checkForm != ""){
  if(!vInteger(checkForm)){
   alert("..........");
   document.base.myName.focus();
   return false;
}
}
}