• jQuery正则的使用方法步骤详解


    本文主要和大家分享的就是jQuery学习中正则的使用,正则在jquery里面并没有比javascript多哪些知识,基本上是一样的,只是选择器更好了一点,一起来看看吧。

    基础正则

    1、正则表达式的创建

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

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

    2、常用规则

    a) 用户密码:/^[a-zA-Z][a-zA-Z0-9_]{5,20}$/

    b) 邮件:/^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$/

    c) 手机:/^[d]{5,20}$/

    d) 其它常用验证:请百度

    3、方式: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('ok');

                }else{

                    alert('wrong');

                }

            });

            // 邮件

            $("#check_email").click(function(){

                var str = $("#t_email").val();

                var ret = /^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$/;

                if(ret.test(str)){

                    alert('ok');

                }else{

                    alert('wrong');

                }

            });

            // 手机

            $("#check_phone").click(function(){

                var str = $("#t_phone").val();

                var ret = /^[d]{5,20}$/;

                if(ret.test(str)){

                    alert('ok');

                }else{

                    alert('wrong');

                }

            });

        });

    </script>

    </html>

    效果演示图

    原文链接:http://www.maiziedu.com/wiki/jquery/regular/

  • 相关阅读:
    Nodejs in Visual Studio Code 06.新建Module
    Nodejs in Visual Studio Code 05.Swig+Bootstrap
    Nodejs in Visual Studio Code 04.Swig模版
    Nodejs in Visual Studio Code 03.学习Express
    Nodejs in Visual Studio Code 02.学习Nodejs
    Nodejs in Visual Studio Code 01.简单介绍Nodejs
    Visual Studio Code 与 Github 集成
    Windows 10 代理上网用户的正确使用姿势
    Visual Studio创建跨平台移动应用_03.AppBuilder Extension
    Visual Studio创建跨平台移动应用_02.Cordova Extension
  • 原文地址:https://www.cnblogs.com/space007/p/6229200.html
Copyright © 2020-2023  润新知