search
HomeWeb Front-endHTML TutorialAbout how to use regular expressions to check tables in HTML

This article mainly introduces the example code of HTML using regular expressions to check tables. The code is simple and easy to understand, very good, and has reference value. Friends who need it can refer to it. I hope it will be helpful to everyone.

<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>xxxx@xxx.xxx  xxx@xxx.xxx.xx</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>

Attachment: Some commonly used rules:

"^\\d+$" //Non-negative integer (positive integer + 0)

"^[0-9]* [1-9][0-9]*$" //Positive integer

"^((-\\d+)|(0+))$" //Non-positive integer (negative Integer + 0)

"^-[0-9]*[1-9][0-9]*$" // Negative integer

"^ -?\\d+$" //Integer

"^\\d+(\\.\\d+)?$" //Non-negative floating point number (positive 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]*))$" //Positive floating point number

"^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$" //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 float Points

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

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

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

"^[a-z]+$"  //A string consisting of 26 lowercase English letters

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

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

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

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

Have you learned it? Hurry up and give it a try.

Related recommendations:

The most complete summary of commonly used regular expressions in the front end

Organization of regular expression registry verification notes

PHP regular expression collection

The above is the detailed content of About how to use regular expressions to check tables in HTML. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the root tag in an HTML document?What is the root tag in an HTML document?Apr 29, 2025 am 12:10 AM

TheroottaginanHTMLdocumentis.Itservesasthetop-levelelementthatencapsulatesallothercontent,ensuringproperdocumentstructureandbrowserparsing.

Are the HTML tags and elements the same thing?Are the HTML tags and elements the same thing?Apr 28, 2025 pm 05:44 PM

The article explains that HTML tags are syntax markers used to define elements, while elements are complete units including tags and content. They work together to structure webpages.Character count: 159

What is the significance of <head> and <body> tag in HTML?What is the significance of <head> and <body> tag in HTML?Apr 28, 2025 pm 05:43 PM

The article discusses the roles of <head> and <body> tags in HTML, their impact on user experience, and SEO implications. Proper structuring enhances website functionality and search engine optimization.

What is the difference between <strong>, <b> tags and <em>, <i> tags?What is the difference between <strong>, <b> tags and <em>, <i> tags?Apr 28, 2025 pm 05:42 PM

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

Please explain how to indicate the character set being used by a document in HTML?Please explain how to indicate the character set being used by a document in HTML?Apr 28, 2025 pm 05:41 PM

Article discusses specifying character encoding in HTML, focusing on UTF-8. Main issue: ensuring correct display of text, preventing garbled characters, and enhancing SEO and accessibility.

What are the various formatting tags in HTML?What are the various formatting tags in HTML?Apr 28, 2025 pm 05:39 PM

The article discusses various HTML formatting tags used for structuring and styling web content, emphasizing their effects on text appearance and the importance of semantic tags for accessibility and SEO.

What is the difference between the 'id' attribute and the 'class' attribute of HTML elements?What is the difference between the 'id' attribute and the 'class' attribute of HTML elements?Apr 28, 2025 pm 05:39 PM

The article discusses the differences between HTML's 'id' and 'class' attributes, focusing on their uniqueness, purpose, CSS syntax, and specificity. It explains how their use impacts webpage styling and functionality, and provides best practices for

What is the 'class' attribute in HTML?What is the 'class' attribute in HTML?Apr 28, 2025 pm 05:37 PM

The article explains the HTML 'class' attribute's role in grouping elements for styling and JavaScript manipulation, contrasting it with the unique 'id' attribute.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),