JAVA中:
可以用異常來(lái)做校驗(yàn)
/** * 判斷字符串是否是整數(shù) */ public static boolean isInteger(String value) { try { Integer.parseInt(value); return true; } catch (NumberFormatException e) { return false; } } /** * 判斷字符串是否是浮點(diǎn)數(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> |
|
來(lái)自: 鳳舞天煌 > 《JAVA技術(shù)》