生活随笔
收集整理的這篇文章主要介紹了
验证是否为数字
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
驗證是否為數字使用javascript的內置函數isNaN()。該函數接受一個字符串類型的參數,如果參數不是數字,isNaN()方法返回true,否則返回false
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script language="javascript"> function check(){var age = document.getElementById("age");if(age.value==null||age.value==""){alert("請輸入年齡!");age.focus();return;}if(isNaN(age.value)){alert("年齡必須為數字!");age.focus();return;}document.getElementById("myform").submit(); }</script>
<body>
<form action="" id="myform">
<table align="center"><tr><td>用戶名:</td><td><input type="text" id="name"></td></tr><tr><td>密碼:</td><td><input type="password" id="pwd"></td></tr><tr><td>確認密碼:</td><td><input type="password" id="pwd1"></td></tr><tr><td>性別:</td><td><input type="radio" name="sex" id="man" value="m">男<input type="radio" name="sex" id="woman" value="f">女</td></tr><tr><td>年齡:</td><td><input type="text" id="age"></td></tr><tr><td align="center" colspan="2"><input type="button" value="提交" onclick="check()"></td></tr>
</table>
</form>
</body>
</html>
總結
以上是生活随笔為你收集整理的验证是否为数字的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。