DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> HTML基礎知識 >> HTML和Xhtml >> HTML用正則表達式檢驗表格的實例代碼
HTML用正則表達式檢驗表格的實例代碼
編輯:HTML和Xhtml     

下面給大家分享HTML用正則表達式檢驗表格的實例代碼,具體代碼內容如下所示:

<span style="font-size:24px;color:#cc6600;"> 正則表達式在JavaScript腳本中是很好用的檢驗語法規則的方法。但是與Java中的正則表達式有所不同。它需要在regex規則上以“^”開始,以"$"結束。</span>  
<span style="font-size:24px;color:#cc6600;">以下讓我們看看一個實例。</span> 
<span style="font-size:18px;"><!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title>Title</title>  
    <style>  
        form table tr td{  
            border: 1px solid lightgrey;  
            text-align: center;  
        }  
        form table tr td input{  
            width: 97%;  
        }  
    </style>  
    <script language="JavaScript" type="text/javascript">  
        //判斷規則(正則表達式)  
    function goto() {  
     var name = document.getElementById("name");  
     var pwd = document.getElementById("pwd");  
     var pwd2 = document.getElementById("pwd2");  
     var pnum = document.getElementById("pnum");  
     var phone = document.getElementById("phone");  
     var telephone = document.getElementById("telephone");  
     var email = document.getElementById("email");  
        if (name.value.trim().length<=8){  
            alert("用戶名長度必須大於八位");  
            name.focus()  
            name.value="";  
            return;  
        }  
//        規則必須包括大小寫字母,數字  
        var regex = /^(?!(?:\d+|[a-zA-Z]+|[\da-z]+|[\dA-Z])$)[\da-zA-Z]{6,}$/;  
//        var regex = /^[A-z0-9]{10,20}$/;  
        if ( !pwd.value.match(regex)){  
            alert("密碼不符合規定");  
            pwd.focus()  
            pwd.value="";  
            return;  
        }  
        if (pwd.value != pwd2.value){  
            alert("兩次輸入的密碼不相同");  
            pwd2.focus()  
            pwd.value="";  
            pwd2.value="";  
            return;  
        }  
        var rege=/^\d{17}X$|^\d{15}$/;  
        if (!rege.test(pnum.value)){  
            alert("身份證不符合規定");  
            pnum.focus()  
            pnum.value="";  
            return;  
        }  
        var regex2 = /^\d{4}-\d{7}$/;//判斷座機號  
        if (!regex2.test(phone.value)){  
            alert("座機號碼不符合規定");  
            phone.focus()  
            phone.value="";  
            return;  
        }  
        var regex3 = /^1[3,5,7,8]\d{9}$/;  
        if (!regex3.test(telephone.value)){  
            alert("手機號碼不符合規定");  
            telephone.focus()  
            telephone.value="";  
            return;  
        }  
        //test方法必須用反斜槓轉義  
        var regex4 = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;  
        if (!regex4.test(email.value)){  
            alert("郵箱不符合規定");  
            email.focus()  
            email.value="";  
            return;  
        }  
    }  
    </script>  
</head>  
<body>  
<form>  
    <table style="width:600px;height: 300px;border: 1px solid lightgrey">  
        <tr>  
            <td width="18%">登錄名:</td>  
            <td><input id="name"type="text"></td>  
            <td width="50%">長度大於八位</td>  
        </tr>  
        <tr>  
            <td>登錄密碼:</td>  
            <td><input   id="pwd"type="password"></td>  
            <td>長度大於十位,包含字母數字</td>  
        </textarea></td>  
        </tr>  
        <tr>  
            <td>確認密碼:</td>  
            <td><input id="pwd2" type="password"></td>  
        </tr>  
        <tr>  
            <td>身份證號碼:</td>  
            <td><input  id="pnum" type="text"></td>  
            <td>15位或18位最後一個是X</td>  
        </tr>  
        <tr>  
            <td>固定電話:</td>  
            <td><input   id="phone"type="text"></td>  
            <td>格式xxxx-xxxxxxx</td>  
        </tr>  
        <tr>  
            <td>手機號碼:</td>  
            <td><input   id="telephone"type="text"></td>  
            <td>11位整數</td>  
        </tr>  
        <tr>  
            <td>電子郵件:</td>  
            <td><input   id="email"type="text"></td>  
            <td>[email protected]  [email protected]</td>  
        </tr>  
        <tr>  
            <td>現居住地:</td>  
            <td><select>  
                <option>--選擇省份--</option>  
                <option>北京</option>  
                <option>河北</option>  
                <option>廣西</option>  
            </select></td>  
            <td><select>  
                <option>--選擇城市--</option>  
                <option>煙台</option>  
                <option>青島</option>  
                <option>哈爾濱</option>  
            </select></td>  
        </tr>  
        <tr>  
            <td colspan="1"></td>  
            <td><input id="submit" type="button" value="提交注冊信息" style="width: 100px" onclick="goto()"></td>  
            <td><input type="reset" value="重置" style="width: 60px"></td>  
        </tr>  
    </table>  
</form>  
</body>  
</html></span> 

附:一些常使用的規則:

"^\\d+$"  //非負整數(正整數 + 0)

"^[0-9]*[1-9][0-9]*$"  //正整數

"^((-\\d+)|(0+))$"  //非正整數(負整數 + 0)

"^-[0-9]*[1-9][0-9]*$"  //負整數

"^-?\\d+$"    //整數

"^\\d+(\\.\\d+)?$"  //非負浮點數(正浮點數 + 0)

"^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮點數

"^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$"  //非正浮點數(負浮點數 + 0)

"^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //負浮點數

"^(-?\\d+)(\\.\\d+)?$"  //浮點數

"^[A-Za-z]+$"  //由26個英文字母組成的字符串

"^[A-Z]+$"  //由26個英文字母的大寫組成的字符串

"^[a-z]+$"  //由26個英文字母的小寫組成的字符串

"^[A-Za-z0-9]+$"  //由數字和26個英文字母組成的字符串

"^\\w+$"  //由數字、26個英文字母或者下劃線組成的字符串

"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"    //email地址

"^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$"  //url

以上所述是小編給大家介紹的HTML用正則表達式檢驗表格的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對網站的支持!

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