DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5詳解
HTML5 註冊頁面完整示例:表單驗證、密碼強度與頭像預覽
編輯:HTML5詳解     

這是一個把 HTML5 新特性一次用上的會員註冊頁範例。它示範了必填驗證(required)、placeholder 提示文字、日期與時間選擇器、以 pattern 限制手機格式、datalist 自動完成、meter 顯示密碼強度、FileReader 實現頭像即時預覽,以及 details 標籤摺疊說明。整個頁面拆成三個檔案:register.html、css/register.css、js/checkbox.js。

一、register.html

<!DOCTYPE html>
<html>
<head>
<title>register.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="Shortcut icon" href="favicon.ico" />
<link rel="stylesheet" type="text/css" href="css/register.css" />
<script src="js/checkbox.js" type="text/javascript"></script>
<script type="text/javascript">
function play(){
  document.getElementById("menu_item").style.display = "";
}
function noplay(){
  document.getElementById("menu_item").style.display = "none";
}

// 依密碼長度更新 meter 與提示文字
function passwd(){
  var pass = document.getElementById("passWord").value;
  var tip  = document.getElementById("tip");
  if (pass.length < 4) {
    document.getElementById("meter").value = pass.length;
    tip.innerHTML = "差";
  } else if (pass.length <= 8) {
    document.getElementById("meter").value = pass.length;
    tip.innerHTML = "中";
  } else {
    document.getElementById("meter").value = pass.length;
    tip.innerHTML = "高";
  }
}
</script>
</head>
<body>
<div id="3" style="position: relative; top: 100px; z-index: 3;">
<form id="f1" action="register.html" method="post">
<table align="center" cellspacing="0" class="table">
<tr class="thead">
  <td align="center">會員註冊</td>
</tr>
<tr>
<td>
<table id="registertable" border="0" align="center" cellspacing="0" cellpadding="5px">

<tr>
  <td align="right">帳號:</td>
  <td align="left">
    <input type="text" name="username" placeholder="用戶名" required />
  </td>
</tr>

<tr>
  <td align="right">密 碼:</td>
  <td align="left">
    <input type="password" name="password" id="passWord"
           placeholder="密碼" required onkeyup="passwd()" />
    <meter min="1" max="10" low="5" high="8" value="0" id="meter"></meter>
    <span id="tip"></span>
  </td>
</tr>

<tr>
  <td align="right">密碼確認:</td>
  <td align="left">
    <input type="password" name="passWord2" placeholder="確認密碼" required />
  </td>
</tr>

<tr>
  <td align="right">生 日:</td>
  <td align="left"><input type="date" name="borthday" /></td>
</tr>

<tr>
  <td align="right">性 別:</td>
  <td align="left">
    <input type="radio" name="gender" value="0" checked />男
    <input type="radio" name="gender" value="1" />女
  </td>
</tr>

<tr>
  <td align="right">郵 箱:</td>
  <td align="left">
    <input type="email" name="email" placeholder="郵箱" id="email" required />
  </td>
</tr>

<tr>
  <td align="right">手 機:</td>
  <td align="left">
    <input type="tel" pattern="[0-9]{11}" id="p" name="phone"
           placeholder="請輸入 11 位數字" />
  </td>
</tr>

<tr>
  <td align="right">地 址:</td>
  <td align="left">
    <input type="text" name="address" placeholder="地址" list="l" />
    <datalist id="l">
      <option value="sh">上海</option>
      <option value="bj">北京</option>
      <option value="js">江蘇</option>
      <option value="zz">鄭州</option>
      <option value="sz">深圳</option>
    </datalist>
  </td>
</tr>

<tr>
  <td align="right">個人網頁:</td>
  <td align="left">
    <input type="url" name="page" placeholder="主頁網址" />
  </td>
</tr>

<tr>
  <td align="right">起床時間:</td>
  <td align="left"><input type="time" name="bed" /></td>
</tr>

<tr>
  <td align="right">頭像:</td>
  <td align="left">
    <input type="file" id="f" accept="image/jpeg" onchange="show()" />
    <span><img id="img" src="" width="60" height="60" /></span>
    <script>
    // 用 FileReader 把選取的圖片即時預覽
    function show(){
      var file = document.getElementById("f").files[0];
      var fileReader = new FileReader();
      fileReader.readAsDataURL(file);
      fileReader.onload = function(){
        document.getElementById("img").src = fileReader.result;
      };
    }
    </script>
  </td>
</tr>

<tr>
  <td colspan="2">
    <details>
      <p>允許註冊 <mark>許可證</mark> 資訊</p>
      <summary>註冊許可資訊</summary>
    </details>
  </td>
</tr>

<tr>
  <td align="right">驗證碼:</td>
  <td valign="middle">
    <input type="text" name="captcha" size="11" maxlength="4"
           title="輸入右邊的驗證碼" />
    <span id="span"></span>
    <script>
    var span = document.getElementById("span");
    span.innerHTML = Math.floor(Math.random() * 9000 + 1000);
    </script>
  </td>
</tr>

<tr height="60px">
  <td align="center" colspan="2">
    <input type="button" value="轉到登錄"
      onclick="window.location.replace('login.html')" id="btn1"
      onmousemove="changeBgColor('btn1')" onmouseout="recoverBgColor('btn1')"
      class="submit" />
    <input type="submit" value="註冊" id="btn"
      onmousemove="changeBgColor('btn')"
      onmouseout="recoverBgColor('btn')" class="submit" formmethod="post" />
  </td>
</tr>

</table>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>

二、css/register.css

body {
  background-image: url("../images/bg.jpg");
  text-align: center;
  background-repeat: repeat-x;
  background-position: 0px 0px;
}
.table {
  border: 1px solid #90BFFF;
  width: 810px;
}
tr {
  font-family: "微軟雅黑";
  font-weight: 800;
  color: #448EF3;
}
input {
  border: 1px solid #448EF3;
  color: #448EF3;
  font-weight: bold;
  font-family: "微軟雅黑";
  height: 35px;
  line-height: 30px;
  border-radius: 5px;
}
.submit {
  width: 150px;
  height: 40px;
  cursor: pointer;
  font-size: 20px;
  color: #ffffff;
  background-color: #448EF3;
  border: 0px;
}
.thead {
  height: 40px;
  background: #90BFFF;
  font-family: "微軟雅黑";
  font-size: 30px;
  font-weight: 700;
  color: #ffffff;
}
#3 {
  margin-bottom: 100px;
}

三、js/checkbox.js

// 全選/取消全選
function ChkAllClick(sonName, cbAllId){
  var arrSon = document.getElementsByName(sonName);
  var cbAll  = document.getElementById(cbAllId);
  var tempState = cbAll.checked;
  for (i = 0; i < arrSon.length; i++) {
    if (arrSon[i].checked != tempState) arrSon[i].click();
  }
}

// 子項目勾選狀態回寫到全選框
function ChkSonClick(sonName, cbAllId) {
  var arrSon = document.getElementsByName(sonName);
  var cbAll  = document.getElementById(cbAllId);
  for (var i = 0; i < arrSon.length; i++) {
    if (!arrSon[i].checked) {
      cbAll.checked = false;
      return;
    }
  }
  cbAll.checked = true;
}

// 反選
function ChkOppClick(sonName){
  var arrSon = document.getElementsByName(sonName);
  for (i = 0; i < arrSon.length; i++) {
    arrSon[i].click();
  }
}

// 按鈕滑入變色
function changeBgColor(btn){
  document.getElementById(btn).style.backgroundColor = "#90BFFF";
}
function recoverBgColor(btn){
  document.getElementById(btn).style.backgroundColor = "#448EF3";
}

頁面預覽

這個範例用到了哪些 HTML5 特性

表單驗證:required 讓瀏覽器自動攔截空值,type="email" 檢查信箱格式,type="url" 檢查網址格式,pattern="[0-9]{11}" 用正則限制手機號碼為 11 位數字。這些都不需要寫一行 JS。

新型輸入控制項:type="date" 與 type="time" 會叫出原生日期/時間選擇器,行動裝置上還會帶入對應的鍵盤配置。

datalist 自動完成:用 list 屬性把輸入框與 datalist 關聯起來,使用者打字時就會出現下拉建議,等於用標籤取代了過去得手寫的 Suggest 元件。

meter 密碼強度:搭配 onkeyup 呼叫的 passwd(),把密碼長度寫進 meter 的 value,並用 low/high 屬性決定顏色區間,同時在旁邊顯示「差/中/高」文字。

FileReader 頭像預覽:選擇檔案後用 readAsDataURL() 讀成 Data URL,直接塞進 img 的 src,使用者立刻看得到自己選的圖,不必先上傳。

details/summary/mark:details 做出可摺疊的說明區塊,summary 是它的標題列,mark 則把「許可證」三個字標示出來。

上 production 前要補的事

這個範例是教學用途,直接上線有幾個地方必須補強。第一,所有驗證都只能算使用者體驗,伺服器端一定要重做一遍,HTML5 驗證可以被 devtools 輕易繞過。第二,「密碼確認」欄位目前沒有任何比對邏輯,需要自行加上 JS 比對。第三,範例中的驗證碼只是用 Math.random() 在畫面上顯示一組數字,完全沒有真正的驗證作用,正式環境必須由後端產生並校對。第四,密碼強度只看長度並不合理,建議同時檢查字元種類與是否為常見弱密碼。最後,<table> 排版與 align、cellpadding 這類舊屬性早已不建議使用,重構時請改用 <form> + <label> + CSS Grid/Flex,並為每個欄位補上 <label for>,這對無障礙與點擊範圍都有實質幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved