HTML5 web Form 的开发实例!
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5表单验证</title>
<link rel="stylesheet" type="text/css" href="../css/style.css">
<link rel="stylesheet" type="text/css" href="../css/test.css">
<script type="text/javascript" src="../js/test.js"></script>
</head>
<body>
<div id="content">
<header>
<h2>HTML5 Web Form</h2>
</header>
<section>
<form method="post" action="" name="myform" class="form" >
<label for="user_name">真实姓名<br/></label>
<input id="user_name" type="text" name="user_name" required pattern="^([\u4e00-\u9fa5]+|([a-z]+\s?)+)$" /><br/> <label for="user_item">比赛项目<br/></label>
<input list="ball" id="user_item" type="text" name="user_item" required/><br/>
<datalist id="ball">
<option value="篮球"/>
<option value="羽毛球"/>
<option value="桌球"/>
</datalist> <label for="user_email">电子邮箱<br/></label>
<input id="user_email" type="email" name="user_email" pattern="^[0-9a-z][a-z0-9\._-]{1,}@[a-z0-9-]{1,}[a-z0-9]\.[a-z\.]{1,}[a-z]$" required /><br/> <label for="user_phone">手机号码<br/></label>
<input id="user_phone" type="telephone" name="user_phone" pattern="^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$" required/><br/> <label for="user_id">身份证号</label><br/>
<input id="user_id" type="text" name="user_id" required pattern="^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$" /><br/> <label for="user_born">出生年月</label><br/>
<input id="user_born" type="month" name="user_born" required /></span><br/> <label for="user_rank">名次期望</label>
<span>第<em id="ranknum">5</em>名</span><br/>
<input id="user_rank" type="range" name="user_rank" value="5" min="1" max="10" step="1" required /><br/> <button type="submit" name="submit" value="提交表单">提交表单</button>
</form>
</section>
<footer><em>Designed by Wang LeHui.</em></footer>
</div>
</body>
</html>
style.css
#content{
margin:0 auto;
width:350px;
}
.form{
padding: 20px;
border:1px solid #bbb;
-moz-border-shadow:0 0 10px #bbb;
-webkit-border-shadow:0 0 10px #bbb;
shadow:0 0 10px #bbb;
}
label+span{
color: #FF0000;
}
.form input,button{
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
background-color: #fff;
border:1px solid #ccc;
font-size: 15px;
width: 250px;
min-height: 30px;
display: block;
margin-bottom: 5px;
margin-top: 5px;
-webkit-border-radius:5px;
-moz-border-radius:5px;
border-radius: 5px;
-webkit-transition:all 0.5s ease-in-out;
-moz-transition:all 0.5s ease-in-out;
transition:all 0.5s ease-in-out;
}
.form input:focus,button:focus{
background-color: #fff;
-moz-border-shadow:0 0 20px #ccc;
-webkit-border-shadow:0 0 20px #ccc;
shadow:0 0 20px #ccc;
-webkit-transform:scale(1.05);
-moz-transform:scale(1.05);
transform:scale(1.05);
}
button[type=submit]{
background-color: #fff;
display: inline-block;
padding:5px 10px 6px 10px;
font-weight: bold;
border:1px solid #888;
border-radius:5px;
-moz-border-radius:5px;
-moz-border-shadow:0 0 3px #888;
-webkit_box_shadow:0 0 3px #888;
box-shadow: 0 0 3px #888;
opacity: 1.0;
}
button[type=submit]:hover{
background-color: #fff;
opacity: 1.0;
color:#516527;
cursor: hand;
cursor: pointer;
}
test.css
.form input:valid{
background:url("../images/right1.png") no-repeat 210px 1px;
} .form input:focus:invalid{
background:url("../images/error1.png") no-repeat 210px 0px;
};
test.js
var rank;
var ranknum;
var user_name;
var user_item;
var user_email;
var email_validityState; function load(){
rank = document.myform.user_rank;
ranknum = document.getElementById('ranknum');
rank.addEventListener("change",changerank,false); user_name = document.getElementById('user_name');
user_name.addEventListener("change",checkname,false); user_item = document.getElementById("user_item");
user_item.addEventListener("change",checkitem,false);
}
function changerank(){
ranknum.innerHTML = rank.value;
}
function checkname(){
if (isname(user_name.value)==null) {
user_name.setCustomValidity("请输入真实的中文名字!");
user_name.validationMessage;
}
else{
user_name.setCustomValidity("");
}
}
function isname(str){
var reg = /^[\u4E00-\u9FA5]{2,5}(?:·[\u4E00-\u9FA5]{2,5})*$/;
return reg.exec(str);
} function checkitem(){
if(isitem(user_item.value)==null){
user_item.setCustomValidity("请正确选择比赛项目!");
user_item.validationMessage;
}
else{
user_item.setCustomValidity("");
}
}
function isitem(str){
if (!(str=="篮球"||str=="桌球"||str=="羽毛球")) {
return null;
}
else {
return 0;
}
}
window.addEventListener("load",load,false);