原文:http://zhidao.baidu.com/question/164832448.html

jsp中要判断,有两种方法:

javascript方法

var re = /^[0-9]+.?[0-9]*$/; //判断字符串是否为数字 //判断正整数 /^[1-9]+[0-9]*]*$/

if (!re.test(input.rate.value))
{
alert("请输入数字(例:0.02)");
input.rate.focus();
return false;
}

java方法

//用JAVA自带的函数
public static boolean isNumeric(String str){
for (int i = str.length();--i>=0;){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}

//用正则表达式
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}

//用ascii码
public static boolean isNumeric(String str){
for(int i=str.length();--i>=0;){
int chr=str.charAt(i);
if(chr<48 || chr>57)
return false;
}
return true;
}
posted on 2011-04-27 10:31  hotty  阅读(1708)  评论(0)    收藏  举报