表单检验输入

<html>
<head>
<script type="text/javascript">
function validate()
{
var at=document.getElementById("email").value.indexOf("@")
var age=document.getElementById("age").value
var fname=document.getElementById("fname").value
submitOK="true"

if (fname.length>10)
 {
 alert("名字必须小于 10 个字符。")
 submitOK="false"
 }
if (isNaN(age)||age<1||age>100)
 {
 alert("年龄必须是 1 与 100 之间的数字。")
 submitOK="false"
 }
if (at==-1) 
 {
 alert("不是有效的电子邮件地址。")
 submitOK="false"
 }
if (submitOK=="false")
 {
 return false
 }
}
</script>
</head>

<body>
<form action="/example/hdom/hdom_submitpage.html" onsubmit="return validate()">
名字(最多 10 个字符):<input type="text" id="fname" size="20"><br />
年龄(从 1 到 100):<input type="text" id="age" size="20"><br />
电子邮件:<input type="text" id="email" size="20"><br />
<br />
<input type="submit" value="提交"> 
</form>
</body>

</html>

猜你喜欢

转载自blog.csdn.net/weixin_44119660/article/details/86683406