JS常見的表單驗證,H5自帶的驗證和正則表達式的驗證

来源:https://www.cnblogs.com/chenyingying0/archive/2020/02/15/12315260.html
-Advertisement-
Play Games

H5驗證 自帶的驗證無法滿足需求: <form action="" method="get"> name:<input type="text" name="name"><br> email:<input type="email" name="email"><br> age:<input type=" ...


H5驗證

自帶的驗證無法滿足需求:

<form action="" method="get">
    name:<input type="text" name="name"><br>
    email:<input type="email" name="email"><br>
    age:<input type="number" name="number"><br>
    <input type="submit">
</form>

 

 幾十輸入不科學的數據依然能夠通過驗證


 

接下來是一個表單驗證的案例

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        body{
            background-color: rgb(231,230,232);
        }
        .container{
            width:1000px;
            margin:50px auto;
            border-radius:5px;
            font-size:12px;
            box-shadow:3px 3px 3px #ccc;
            color:#333;
        }
        .container h3{
            background-color: rgb(97,134,213);
            text-align: center;
            color:#fff;
            padding:10px;
            border-radius:5px 5px 0 0;
            letter-spacing: .5em;
        }
        .item{
            height:49px;
            padding:10px 50px;
            background-color: #fff;
            border-bottom:1px solid #444;
            position: relative;
        }
        .item label{
            font-weight:bold;
            display: inline-block;
            margin-top:10px;
        }
        .item input{
            width:400px;
            height:20px;
            line-height:20px;
            border-radius:5px;
            border:1px solid #ccc;
            padding:5px;
            position: absolute;
            left:150px;
        }
        .item span{
            color:rgb(97,134,213);
        }
        /*設置placeholder字體顏色*/
        input::-webkit-input-placeholder {      /* WebKit browsers */
            color: #ccc;
            font-size:12px;
        }
        input:-moz-placeholder {                /* Mozilla Firefox 4 to 18 */
            color: #ccc;
            font-size:12px;
        }
        input::-moz-placeholder {               /* Mozilla Firefox 19+ */
            color: #ccc;
            font-size:12px;
        }
        input:-ms-input-placeholder {           /* Internet Explorer 10+ */
            color: #ccc;
            font-size:12px;
        }
        .submit-box{
            text-align: center;
            height:50px;
            line-height: 50px;
            padding:5px;
            background-color: #fff;    

        }
        .submit-box .check{
            margin-right:10px;
        }
        .submit-box .submit{
            width:100px;
            height:30px;
            padding:5px;
            background-color: rgb(97,134,213);
            color:#fff;
            text-align: center;
            border-radius: 5px;
            border:1px solid #ddd;
            margin-left:10px;
            font-size:12px;    
        }
        .tip{
            margin-top:5px;
        }
    </style>
</head>
<body>
<div class="container">
    <h3>---賬戶信息---</h3>
    <form action="" id="form">
        <div class="item">
            <label for=""><span>*</span> 用戶名:</label>
            <input type="text" name="username" id="username" placeholder="用戶設置成功後不可修改">
            <p class="tip"></p>
        </div>
        <div class="item">
            <label for=""><span>*</span> 登錄密碼:</label>
            <input type="password" name="pwd" id="pwd" placeholder="6-20位字母,數字或符號">
            <p class="tip"></p>
        </div>
        <div class="item">
            <label for=""><span>*</span> 確認密碼:</label>
            <input type="password" name="repwd" id="repwd" placeholder="請再次輸入密碼">
            <p class="tip"></p>
        </div>
        <div class="item">
            <label for=""><span>*</span> 姓名:</label>
            <input type="text" name="Cname" id="Cname" placeholder="請輸入姓名,中文且最多五位">
            <p class="tip"></p>
        </div>
        <div class="item">
            <label for=""><span>*</span> 身份證號:</label>
            <input type="text" name="idNum" id="idNum" placeholder="請輸入身份證號">
            <p class="tip"></p>
        </div>
        <div class="item">
            <label for=""><span>*</span> 郵箱:</label>
            <input type="text" name="email" id="email" placeholder="請輸入正確郵箱格式">
            <p class="tip"></p>
        </div>
        <div class="item">
            <label for=""><span>*</span> 手機號碼:</label>
            <input type="text" name="phone" id="phone" placeholder="請輸入您的手機號碼">
            <p class="tip"></p>
        </div>
        <div class="submit-box">
            <input type="checkbox" class="check">
            <span>我已閱讀並同意遵守規定</span>
            <input type="submit" value="確定提交" class="submit" id="submit">
        </div>
    </form>
</div>

<script src="index.js"></script>
</body>
</html>

index.js

var username=document.getElementById("username");
var pwd=document.getElementById("pwd");
var repwd=document.getElementById("repwd");
var Cname=document.getElementById("Cname");
var idNum=document.getElementById("idNum");
var email=document.getElementById("email");
var phone=document.getElementById("phone");

var submit=document.getElementById("submit");
var form=document.getElementById("form");

var tips=document.querySelectorAll(".tip");
var reg=/正則/;
var test1=false;
var test2=false;
var test3=false;
var test4=false;
var test5=false;
var test6=false;
var test7=false;

//驗證用戶名,失去焦點時觸發
username.onblur=function(){
    var pattern=/^\w{6,18}$/;
    if(this.value==""){//this指代當前輸入框
        tips[0].innerHTML="用戶名不能為空";
        tips[0].style.color="red";
        test1=false;
    }else{
        if(pattern.exec(this.value)==null){
            tips[0].innerHTML="請輸入6-18位數字、字母或下劃線";
            tips[0].style.color="red";
            test1=false;
        }else{
            tips[0].innerHTML="格式正確";
            tips[0].style.color="green";
            test1=true;
        }
    }
}

//驗證密碼,失去焦點時觸發
pwd.onblur=function(){
    var pattern=/^\w{6,18}$/;
    if(this.value==""){//this指代當前輸入框
        tips[1].innerHTML="密碼不能為空";
        tips[1].style.color="red";
        test2=false;
    }else{
        if(pattern.exec(this.value)==null){
            tips[1].innerHTML="請輸入6-18位數字、字母或下劃線";
            tips[1].style.color="red";
            test2=false;
        }else{
            tips[1].innerHTML="格式正確";
            tips[1].style.color="green";
            test2=true;
        }
    }
}

//確認密碼,失去焦點時觸發
repwd.onblur=function(){
    if(this.value==""){//this指代當前輸入框
        tips[2].innerHTML="確認密碼不能為空";
        tips[2].style.color="red";
        test3=false;
    }else{
        if(this.value!=pwd.value){
            tips[2].innerHTML="兩次密碼不一致";
            tips[2].style.color="red";
            test3=false;
        }else{
            tips[2].innerHTML="格式正確";
            tips[2].style.color="green";
            test3=true;
        }
    }
}

//驗證中文名,失去焦點時觸發
Cname.onblur=function(){
    var pattern=/^[\u4e00-\u9fa5]{2,5}$/;
    if(this.value==""){//this指代當前輸入框
        tips[3].innerHTML="姓名不能為空";
        tips[3].style.color="red";
        test4=false;
    }else{
        if(pattern.exec(this.value)==null){
            tips[3].innerHTML="請輸入2-5位中文名";
            tips[3].style.color="red";
            test4=false;
        }else{
            tips[3].innerHTML="格式正確";
            tips[3].style.color="green";
            test4=true;
        }
    }
}

//驗證身份證,失去焦點時觸發
idNum.onblur=function(){
    var pattern=/^\d{17}[0-9x]$/;
    if(this.value==""){//this指代當前輸入框
        tips[4].innerHTML="身份證不能為空";
        tips[4].style.color="red";
        test5=false;
    }else{
        if(pattern.exec(this.value)==null){
            tips[4].innerHTML="身份證格式錯誤";
            tips[4].style.color="red";
            test5=false;
        }else{
            tips[4].innerHTML="格式正確";
            tips[4].style.color="green";
            test5=true;
        }
    }
}

//驗證郵箱,失去焦點時觸發
email.onblur=function(){
    //[email protected]
    var pattern=/^\w+@\w+(\.[a-zA-Z_]{2,4})+$/;
    if(this.value==""){//this指代當前輸入框
        tips[5].innerHTML="郵箱不能為空";
        tips[5].style.color="red";
        test6=false;
    }else{
        if(pattern.exec(this.value)==null){
            tips[5].innerHTML="郵箱格式錯誤";
            tips[5].style.color="red";
            test6=false;
        }else{
            tips[5].innerHTML="格式正確";
            tips[5].style.color="green";
            test6=true;
        }
    }
}

//驗證手機號,失去焦點時觸發
phone.onblur=function(){
    var pattern=/^\d{11}$/;
    if(this.value==""){//this指代當前輸入框
        tips[6].innerHTML="手機號不能為空";
        tips[6].style.color="red";
        test7=false;
    }else{
        if(pattern.exec(this.value)==null){
            tips[6].innerHTML="手機號格式錯誤";
            tips[6].style.color="red";
            test7=false;
        }else{
            tips[6].innerHTML="格式正確";
            tips[6].style.color="green";
            test7=true;
        }
    }
}

//前面所有數據正確時,才能提交
submit.onclick=function(){
    if(test1 && test2 && test3 && test4 && test5 && test6 && test7){
        form.submit();
    }else{
        alert("信息填寫有誤");
    }
}

效果圖

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 創建 test 測試表 CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `c1` varchar(10) DEFAULT NULL, `c2` varchar(10) DEFAULT NULL, `c3` varchar(10) ...
  • 1、何為explain執行計劃? 使用explain關鍵字可以模擬優化器執行SQL語句,從而知道MySQL是如何使用索引來處理你的SQL查詢語句以及連接表,可以分析查詢語句或是結構的性能瓶頸,幫助我們選擇更好的索引和寫出更優化的查詢語句。(說白了,就是優化SQL的工具) 2、如何使用explain? ...
  • 解決的需求 有些時候不能在NDK環境編譯,或者使用NDK編譯會頗費周折,然後又想使用Android系統自帶的Log類方法,那麼我們就可以使用 dlopen 來實現我們的目的。比如在OpenCV中添加Android的Log列印。 關於dlopen 1. dlopen和dlclose對處理的lib進行引 ...
  • 小程式註冊 微信小程式開發之前,必須先有小程式賬號,下麵是註冊步驟: 1、打開網頁 微信公眾平臺https://mp.weixin.qq.com/,點擊立即註冊按鈕 2、註冊類型 選擇小程式開發 3、小程式註冊 填寫自己的註冊信息 註意這裡每個郵箱僅能申請一個小程式 4、郵箱激活 5、信息登記 我自 ...
  • 一、 形變中心點介紹 <style> ul li { width: 100px; height: 100px; list-style: none; float:left; margin:0 auto; /*transform-origin:200px 0px;*/ transform-origin: ...
  • 實驗準備 1. 在伺服器部署 作為我們直播推流和拉流的伺服器(如果服務商選擇七牛,也是直接給地址推流)。為了加快部署,我在這一步使用Docker。 2. 記下推流地址(我本地搭建的: ) 3. 新建Uniapp項目 4. 點擊項目下方的 文件,點擊APP常用其他設置 去除V3編譯器 (Hbuilde ...
  • 第一部分:樣式規則 塊級元素一行只有一個,比如P標簽 CSS層疊樣式表,意思就是樣式是可以疊加的,比如下麵的代碼 <style> .ok{ color: aqua; } .blue{ color: #5283ff; }/*p名詞叫標簽選擇器*/ p{ color: black; font-size: ...
  • 第一種 使用一個標簽 第二種 使用 script 標簽,引入一個外部的 js 文件 需要創建一個js文件寫js代碼 使用第二種方式時候,不需要在script標簽裡面寫js代碼 第三種: 與HTML標簽中屬性結合 註意事項 script標簽用於引入外部文件,就不能再編寫其他script代碼了,即使編寫 ...
一周排行
    -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.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...