HTML5表單驗證(4個實用的表單美化案例)

来源:https://www.cnblogs.com/chenyingying0/archive/2020/01/06/12156484.html
-Advertisement-
Play Games

multipart/form-data 在使用包含文件上傳控制項的表單時,必須使用autocomplete="on" 自動補全功能novalidate 不驗證 <form enctype="multipart/form-data" novalidate autocomplete="on"></form ...


multipart/form-data 在使用包含文件上傳控制項的表單時,必須使用
autocomplete="on" 自動補全功能
novalidate 不驗證

    <form enctype="multipart/form-data" novalidate autocomplete="on"></form>

placeholder

required 必填
autofocus 預設聚焦
pattern 正則驗證

        <input type="text" name="gonghao" required autofocus placeholder="請輸入工號" pattern="^\d{5}[imooc]$">

dataList對選擇框的記憶
list記憶內容

        <input type="text" list="tips">
        <dataList id="tips">
            <option value="erwerewr"></option>
            <option value="erwerew2r"></option>
            <option value="erwerew2r"></option>
            <option value="erwerewr"></option>
        </dataList>

html5約束驗證API

id.validity獲取驗證約束

    console.log(username.validity);

如果輸入值長度大於要求的長度,則截取要求的長度部分

    <input type="number" id="numbers" oninput="checkLength(this,5)" step="0.01" />
    function checkLength(obj, length) {
        if (obj.value.length > length) {
            obj.value = obj.value.substr(0, length);
        }
    }

valueMissing => required
typeMismatch => HTML類型,如email
rangeUnderflow => min

        <input type="text" required pattern="^\d{4}$" oninput="checkit(this)" placeholder="請輸入代碼">
-----------------------
        function checkit(obj){
            console.log(obj.validity);
            var it = obj.validity;
            if (true===it.valueMissing) {
                obj.setCustomValidity("不能為空");
            }else{
                if (true===it.patternMismatch) {
                    obj.setCustomValidity("必須是4個數字");
                }else{
                    obj.setCustomValidity("");
                }
            }
        }

checkValidity()滿足約束返回true,否則false

    if (username.checkValidity()) {
        alert("用戶名符合");
    } else {
        alert("不符合");
    }

自帶驗證美化
:required
:optional

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .container{
            max-width:400px;
            margin:20px auto;
        }
        input,select,textarea{
            width: 240px;
            margin:10px 0;
            border:1px solid #999;
            padding: .5em 1em;
        }
        label{
            color:#999;
            margin-left: 10px;
        }
        input:required,textarea:required{
            border-right:3px solid #aa0088;
        }
        input:optional,select:optional{
            border-right:3px solid #999;
        }
        input:required +label::after{
            content: "(必填)"
        }
        input:optional +label::after{
            content: "(選填)"
        }
        input:focus,select:focus,textarea:focus{
            outline:0;
        }
        input:required:focus,textarea:required:focus{
            box-shadow: 0 0 3px 1px #aa0088;
        }
        input:optional:focus,select:required:focus{
            box-shadow: 0 0 3px 1px #999;
        }
        input[type="submit"]{
            background-color: #cc00aa;
            border: 2px solid #aa0088;
            padding:10px 0;
            color:#fff;
        }
        input[type="submit"]:hover{
            background-color:#aa0088;
        }
    </style>
</head>
<body>
    <!-- <div class="contenteditable"></div> -->
    <div class="container">
        <form action="#">
            <input type="name" required><label>名稱</label>
            <input type="email" required><label>郵箱</label>
            <input type="tel"><label>手機</label>
            <input type="url"><label>網址</label>
            <select name="" id="">
                <option value="">非必選項一</option>
                <option value="">非必選項二</option>
                <option value="">非必選項三</option>
                <option value="">非必選項四</option>
            </select>
            <textarea name="#" cols="30" rows="10" placeholder="留言(必填)" required></textarea>
            <input type="submit" value="提交表單">
        </form>
    </div>
</body>
</html>

:valid
:invalid

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>純CSS表單驗證美化(invalid和valid)應用案例</title>
    <style>
        .container{
            margin:100px;
            position:relative;
        }
        input{
            border: 1px solid #999;
            outline:0;
            width:140px;
            height:30px;
            line-height:30px;text-indent:36px;transition: all .3s;
            border-radius:5px;
        }
        span.title{
            position:absolute;
            line-height:30px;
            height: 30px;
            left:2px;
            top:2px;
            transition:all .3s;
        }
        input:focus,input:hover{
            text-indent:2px;
        }
        input:focus+span.title,input:hover+span.title{
            transform:translateX(-120%)
        }
        input:valid ~label::after{
            content: "你輸入正確!"
        }
        input:invalid ~label::after{
            content: "你郵箱錯誤!"
        }
        input:valid{
            border:1px solid green;
        }
        input:invalid{
            border:1px solid red;
        }
    </style>
</head>
<body>
    <div class="container">
        <input id="mail" type="email" required placeholder="輸入郵箱">
        <span class="title">郵箱</span>
        <label for="mail"></label>
    </div>
</body>
</html>

oninvalid
oninput

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>html5表單美化綜合案例</title>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1">
</head>
<style>
.onelist {
    margin: 10px 0 5px 12px;
}

.onelist label {
    width: 80px;
    display: inline-block
}

.onelist input,
.onelist select {
    height: 25px;
    line-height: 25px;
    width: 220px;
    border-radius: 3px;
    border: 1px solid #e2e2e2;
}

.onelist input[type="submit"] {
    width: 150px;
    height: 30px;
    line-height: 30px;
}

select:required,
input:required,
textarea:required {
    background: #fff url(../img/star.jpg) no-repeat 99% center;
}

select:required:valid,
input:required:valid,
textarea:required:valid {
    background: #fff url(../img/right.png) no-repeat 99% center;
    box-shadow: 0 0 5px #5cd053;
    border-color: #28921f;
}

select:focus:invalid,
input:focus:invalid,
textarea:focus:invalid {
    background: #fff url(../img/error.png) no-repeat 99% center;
    box-shadow: 0 0 5px red;
    border-color: red;
    outline: red solid 1px;
}
</style>

<body>
    <form class="myform" onsubmit="return checkForm();" method="post">
        <div class="onelist">
            <label for="UserName">手機號</label>
            <input name="UserName" id="UserName" type="text" placeholder="請輸入手機號碼" pattern="^1[0-9]{10}$" required oninvalid="this.setCustomValidity('請輸入正確的號碼');" oninput="setCustomValidity('')">
        </div>
        <div class="onelist">
            <label for="Password">密碼</label>
            <input name="Password" id="Password" type="password" placeholder="6~20位" class="" pattern="^[a-zA-Z0-9]\w{5,19}$" required oninvalid="this.setCustomValidity('請輸入正確密碼');" oninput="setCustomValidity('')" onchange="checkPassword()">
        </div>
        <div class="onelist">
            <label for="Repassword">確認密碼</label>
            <input name="Repassword" id="Repassword" type="password" placeholder="確認密碼" class="" required onchange="checkPassword()">
        </div>
        <div class="onelist">
            <label for="Repassword">瞭解方式</label>
            <select name="konw" required>
                <option value="">==請選擇==</option>
                <option value="1">搜索引擎</option>
                <option value="2">朋友圈</option>
                <option value="3">朋友推廣</option>
                <option value="4">廣告投放</option>
            </select>
        </div>
        <div class="onelist">
            <input type="submit" value="提交">
        </div>
    </form>
    <script type="text/javascript">
    function checkPassword() {
        var pass1 = document.getElementById("Password");
        var pass2 = document.getElementById("Repassword");

        if (pass1.value != pass2.value)
            // 設置自定義驗證約束提示信息
            pass2.setCustomValidity("兩次輸入的密碼不匹配");
        else
            pass2.setCustomValidity("");
    }
    </script>
</body>
</html>

預設氣泡修改
event.preventDefault(); 阻止預設氣泡

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
    <title>表單驗證預設樣式修改</title>
</head>
<style>
    .oneline{line-height: 1.5;margin:10px auto;}
    .oneline label{width:100px;text-indent: 15px;font-size:14px;font-family: "Microsoft Yahei";display: inline-block;}
    .oneline .sinput{width:60%;height:30px;border-radius: 6px;border:1px solid #e2e2e2;}
    .oneline input[type="submit"]{margin-left:20px;width:80px;height:30px;border:0;background-color:#5899d0;color:#fff;font-size:14px;border-radius: 6px;}
    .error-message{color:red; font-size:12px;text-indent:108px;}
</style>

<body>
    <form id="forms">
        <div class="oneline">
            <label for="name">用戶名:</label>
            <input id="name" name="name" class="sinput" required>
        </div>
        <div class="oneline">
            <label for
              
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 逆推繼承看原型 function F1(age) { this.age = age; } function F2(age) { this.age = age; } F2.prototype = new F1(10); function F3(age) { this.age = age; } F3.p ...
  • 總結繼承 面向對象特性: 封裝, 繼承,多態 繼承, 類與類之間的關係, 面向對象的語言的繼承是為了多態服務的 js不是面向對象的語言, 但是可以模擬面向對象,模擬繼承,為了節省記憶體 繼承: 原型作用: 數據共用, 目的是: 為了節省記憶體空間, 原型作用: 繼承 目的是:為了節省記憶體空間 原型繼承: ...
  • 拷貝繼承:把一個對象中的屬性或者方法直接複製到另一個對象中 淺拷貝 function Person() { } Person.prototype.age = 10; Person.prototype.sex = "男"; Person.prototype.height = 100; Person.p ...
  • 註釋單行註釋:// 快捷鍵: CTRL + / 多行註釋: /* 內容 */ 快捷鍵: ctrl + shift + /變數申明變數var name;賦值name = 'peach';初始化變數var age=18;更新變數var age_age = 18; age_age=20; // 更新申明多... ...
  • 創建canvas <canvas id="myCanvas" class="canvas"> 您的瀏覽器不支持canvas </canvas> 基礎設置 <script type="text/javascript"> var canvas = document.getElementById('myC ...
  • JSONP Hijackin的中文意思是JSON劫持,而能產生JSON數據劫持的原因在於前端被跨站攻擊了。跨站=跨域,跨域從字面上理解的話,就是指超出了範圍、領域。繼續追問一下,那超出了什麼範圍?原來指的範圍有多大?理解跨站攻擊的基礎在於理解這個域有多大。為了更準確的理解JSON Hijackin攻 ...
  • web前端工程師是近幾年的新興職業,也是目前火爆而且高薪的職業。不同的公司也有不同的叫法,比如:網頁界面開發,網站設計等,要學好web前端開發,需要掌握什麼方法與技巧? 一、div和table 這個是最簡單的,也是最基礎的。要熟練掌握div、form table、ul li 、p、span、font ...
  • 一、文本的水平對齊方式 使用text align來設置文本的對齊方式;text align的取值:left(向左對齊)/center(水平居中對齊)/right(向右對齊)/justify(兩端對齊); text align只對應用此樣式的元素的非塊級子元素有效,對塊級子元素無效;對比使用margi ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...