/*** * 判断字符串是否为数字(包含小数点)
* @param str
* @return
*/
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]+.*[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}
js:
function isNumeric(str){ var re = /^(([1-9][0-9]*.[0-9][0-9]*)|([0].[0-9][0-9]*)|([1-9][0-9]*)|([0]{1}))$; //判断字符串如果是整数不能以0开头后面加正整数,如果是浮点数整数部分不能为两个0:如00.00,如果是整数, var b; if (re.exec(str)==null){ b= false; }else{ b= true; } return b; }
^d+$/ //非负整数(正整数 + 0) /^[0-9]*[1-9][0-9]*$/ //正整数 /^((-d+)|(0+))$/ //非正整数(负整数 + 0) /^-[0-9]*[1-9][0-9]*$/ //负整数 /^-?d+$/ //整数 /^d+(.d+)?$/ //非负浮点数(正浮点数 + 0) /^(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*))$/ //正浮点数 /^((-d+(.d+)?)|(0+(.0+)?))$/ //非正浮点数(负浮点数 + 0) /^(-(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*)))$/ //负浮点数 /^(-?d+)(.d+)?$/ //浮点数