<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>表单验证</title>
<style>
.star{
color:red;
}
.dt{
padding-top:10px;
}
</style>
<script type="text/javascript" src="../js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(function(){
$("form :input.required").each(function(){
var $required = $("<strong class='star'>*</strong>"); // 创建元素
$(this).parent().append($required); // 将其追加到文档中
})
$("form :input").blur(function(){
if($(this).is("#username")){ // 判断元素id是否为用户名的文本框
if($(this).val() == ""){ // 判断用户名是否为空
alert("用户名不能为空!");
}
}
if($(this).is("#pwd")){ // 判断是否为密码框
if($(this).val() == ""){ // 判断密码是否为空
alert("密码不能为空!");
}
if(this.value.length < 8){ // 判断密码的长度是否小于8
alert("密码不能小于8位,请重新输入!");
}
}
})
})
</script>
</head>
<body>
<form>
<h3 align="center">用户注册</h3>
<div class="dt">用户名:<input type="text" id="username" name="username" size=20 class="required" /></div>
<div class="dt">密 码:<input type="password" id="pwd" name="pwd" size=20 class="required" /></div>
<div class="dt">性 别:<input type="text" id="sex" name="sex" size=4 maxlength=3 /></div>
<div class="dt">年 龄:<input type="text" id="age" name="age" size=4 maxlength=3 /></div>
<div class="dt">
<input type="submit" name="sub" value="注册" />
</div>
</form>
</body>
</html>
表单验证
最新推荐文章于 2024-04-30 08:58:14 发布