Home > Web Front-end > JS Tutorial > How to use regular expressions in jquery

How to use regular expressions in jquery

coldplay.xixi
Release: 2023-01-04 09:36:54
Original
2983 people have browsed it

Methods used by jquery regularity: 1. User password, code is [/^[a-zA-Z][a-zA-Z0-9_]{5,20}$/]; 2. Email , the code is [/^[\w-] (\.[\w-] )*@[\w-] (\.[\w-] ) $/].

How to use regular expressions in jquery

The operating environment of this tutorial: windows7 system, jquery3.2.1 version, thinkpad t480 computer.

Recommended: jquery video tutorial

##Methods used by jquery regular:

1. Creation of regular expression

a)

var checkNum = /^[A-Za-z0-9] $/;

b)

var re=new RegExp("[" s1 "]","g");

2. Common rules

a) User password:

/^[a-zA-Z][a-zA-Z0-9_]{5,20}$/

b) Email:

/^[\w-] (\.[\w-] )*@[\w-] (\.[\w-] ) $/

c) Mobile phone:

/^[\d]{5,20}$/

d) Other commonly used verifications: please Baidu

3, method: test

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>jquery ajax</title>
    <script type="text/javascript" src="public/js/jquery-2.2.3.min.js"></script>
</head>
<body>
<form action="">
    <label>用户名:</label><span id="check_username">检测</span>
    <input type="text" id="t_username" placeholder="请输入"/>
    <hr/>
    <label>邮箱:</label><span id="check_email">检测</span>
    <input type="text" id="t_email" placeholder="请输入"/>
    <hr/>
    <label>手机:</label><span id="check_phone">检测</span>
    <input type="text" id="t_phone" placeholder="请输入"/>
    <hr/>
</form>
</body>
<script>
    $(function () {
        // 用户名
        $("#check_username").click(function(){
            var str = $("#t_username").val();
            var ret = /^[a-zA-Z][a-zA-Z0-9_]{5,20}$/;
            if(ret.test(str)){
                alert(&#39;ok&#39;);
            }else{
                alert(&#39;wrong&#39;);
            }
        });
        // 邮件
        $("#check_email").click(function(){
            var str = $("#t_email").val();
            var ret = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
            if(ret.test(str)){
                alert(&#39;ok&#39;);
            }else{
                alert(&#39;wrong&#39;);
            }
        });
        // 手机
        $("#check_phone").click(function(){
            var str = $("#t_phone").val();
            var ret = /^[\d]{5,20}$/;
            if(ret.test(str)){
                alert(&#39;ok&#39;);
            }else{
                alert(&#39;wrong&#39;);
            }
        });
    });
</script>
</html>
Copy after login

Related free learning recommendations: javascript(Video)

The above is the detailed content of How to use regular expressions in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template