JAVA中:
可以用异常来做校验
/** * 判断字符串是否是整数 */ public static boolean isInteger(String value) { try { Integer.parseInt(value); return true; } catch (NumberFormatException e) { return false; } } /** * 判断字符串是否是浮点数 */ public static boolean isDouble(String value) { try { Double.parseDouble(value); if (value.contains(".")) return true; return false; } catch (NumberFormatException e) { return false; } } /** * 判断字符串是否是数字 */ public static boolean isNumber(String value) { return isInteger(value) || isDouble(value); } javascript中:
<form name="a" action="" method="post" onSubmit=check() >
.... </form> <script language=vbscript> if not isnumeric(a.text.value) then alert("只能是数字!") window.event.returnvalue=false end if </script> |
|