1. ホーム
  2. Web制作
  3. HTML/Xhtml

テーブルのサンプルコードをチェックするためのHTML正規表現

2022-01-25 04:49:31

以下は、HTML正規表現のテストフォームを共有するためのサンプルコードで、具体的なコード内容は以下の通りです。

<span style="font-size:24px;color:#cc6600;"> Regular expressions are good to use in JavaScript scripts to check syntax rules. But it is different from regular expressions in Java. It needs to start with "^" on the regex rule and end with "$". </span>  

<span style="font-size:24px;color:#cc6600;"> Let's look at an example below. </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">  
        // Judgment rules (regular expressions)  
    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("username length must be greater than eight ");  
            name.focus()  
            name.value="";  
            return;  
        }  
// rules must include upper and lower case letters, numbers  
        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("Password does not match");  
            pwd.focus()  
            pwd.value="";  
            return;  
        }  
        if (pwd.value ! = pwd2.value){  
            alert("The password entered twice is not the same");  
            pwd2.focus()  
            pwd.value="";  
            pwd2.value="";  
            return;  
        }  
        var rege=/^\d{17}X$|^\d{15}$/;  
        if (!rege.test(pnum.value)){  
            alert("ID does not match");  
            pnum.focus()  
            pnum.value="";  
            return;  
        }  
        var regex2 = /^\d{4}-\d{7}$/;//judge the landline number  
        if (!regex2.test(phone.value)){  
            alert("Landline number does not match");  
            phone.focus()  
            phone.value="";  
            return;  
        }  
        var regex3 = /^1[3,5,7,8]\d{9}$/;  
        if (!regex3.test(telephone.value)){  
            alert("phone number does not match");  
            telephone.focus()  
            telephone.value="";  
            return;  
        }  
        //test method must be escaped with a backslash  
        var regex4 = /^\w+([-+.] \w+)*@\w+([-.] \w+)*\. \w+([-.] \w+)*$/;  
        if (!regex4.test(email.value)){  
            alert("Mailbox does not match");  
            email.focus()  
            email.value="";  
            return;  
        }  
    }  
    </script>  
</head>  
<body>  
<form>  
    <table style="width:600px;height: 300px;border: 1px solid lightgrey">  
        <tr>  
            <td width="18%">login name:</td>  
            <td><input id="name"type="text"></td>  
            <td width="50%">length greater than eight bits</td>  
        </tr>  
        <tr>  
            <td>Login Password:</td>  
            <td><input id="pwd"type="password"></td>  
            <td> greater than ten digits in length, contains alphanumeric </td>  
        </textarea></td>  
        </tr>  
        <tr>  
            <td> Confirm Password:</td>  
            <td><input id="pwd2" type="password"></td>  
        </tr>  
        <tr>  
            <td>ID#:</td>  
            <td><input id="pnum" type="text"></td>  
            <td>15 or 18 bits last is X</td>  
        </tr>  
        <tr>  
            <td>Landline:</td>  
            <td><input id="phone"type="text"></td>  
            <td>format xxxx-xxxxxxx</td>  
        </tr>  
        <tr>  
            <td>Mobile Number:</td>  
            <td><input id="telephone"type="text"></td>  
            <td>11-bit integer</td>  
        </tr>  
        <tr>  
            <td>email:</td>  
            <td><input id="email"type="text"></td>  
            <td>[email protected] [email protected]</td>  
        </tr>  
        <tr>  
            <td>" //non-positive floating point number (negative floating point number + 0) 


"^(-(([0-9]+\. [0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\\. [0-9]+)|([0-9]*[1-9][0-9]*)))$" //negative floating point number

"^(-? \\\d+)( \\\. \\\d+)? $ " //floating point number

"^[A-Za-z]+$" // string consisting of 26 letters of the alphabet

"^[A-Z]+$" // string consisting of 26 letters of the alphabet in uppercase

"^[a-z]+$" // a string consisting of 26 letters in lower case

"^[A-Za-z0-9]+$" // a string consisting of numbers and 26 letters of the alphabet

"^\\w+$" // a string consisting of numbers, 26 letters or underscores

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

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

The above is an example code of HTML with regular expressions to check the form, I hope it will help you, if you have any questions please leave me a message, I will reply to you in time. Here also thank you very much for the support of the script home website!