JAVA中:
可以用異常來做校驗
/** * 判斷字符串是否是整數(shù) */ public static boolean isInteger(String value) { try { Integer.parseInt(value); return true; } catch (NumberFormatException e) { return false; } } /** * 判斷字符串是否是浮點數(shù) */ public static boolean isDouble(String value) { try { Double.parseDouble(value); if (value.contains(".")) return true; return false; } catch (NumberFormatException e) { return false; } } /** * 判斷字符串是否是數(shù)字 */ 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("只能是數(shù)字!") window.event.returnvalue=false end if </script> |
|