定制开发小程序JavaScript正则表达式实现注册信息校验

Java和JavaScript的对比

  • Java定制开发小程序中也有正则表达式,定制开发小程序默认情况下必须要精确匹配 ;而在JS定制开发小程序中默认是模糊匹配,定制开发小程序只要字符串包含了正则表达式的内容就返回true
正则表达式匹配字符串Java中匹配结果JavaScript中匹配结果
\d{3}a123bfalsetrue
^\d{3}123bfalsetrue
\d{3}$a123falsetrue
^\d{3}$123truetrue

注册信息校验

需求

  1. 在JS中使用正则表达式进行验证。
  2. 用户名:只能由英文字母和数字组成,长度为4~16个字符,并且以英文字母开头
  3. 密码: 大小写字母和数字6-20个字符
  4. 确认密码:两次密码要相同
  5. 电子邮箱: 符合邮箱地址的格式 /^\w+@\w+(.[a-zA-Z]{2,3}){1,2}$/
  6. 手机号:/^1[34578]\d{9}$/
  7. 生日:生日的年份在1900~2009之间,生日格式为1980-5-12或1988-05-04的形式,/^((19\d{2})|(200\d))-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])$/

案例分析

  1. 创建正则表达式
  2. 得到文本框中输入的值
  3. 如果不匹配,在后面的span中显示错误信息,返回false
  4. 如果匹配,在后面的span中显示一个打勾图片,返回true
  5. 写一个验证表单中所有的项的方法,所有的方法都返回true,这个方法才返回true.
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>验证注册页面</title>    <style type= "text/css">        body {            margin: 0;            padding: 0;            font‐size: 12px;            line‐height: 20px;        }        .main {            width: 525px;            margin‐left: auto;            margin‐right: auto;        }        .hr_1 {            font‐size: 14px;            font‐weight: bold;            color: #3275c3;            height: 35px;            border‐bottom‐width: 2px;            border‐bottom‐style: solid;            border‐bottom‐color: #3275c3;            vertical‐align: bottom;            padding‐left: 12px;        }        .left {            text‐align: right;            width: 80px;            height: 25px;            padding‐right: 5px;        }        .center {            width: 280px;        }        .in {            width: 130px;            height: 16px;            border: solid 1px #79abea;        }        .red {            color: #cc0000;            font‐weight: bold;        }        div {            color: #F00;        }        </style>    <script type="text/javascript">        //验证表单中所有的项        function checkAll () {//所有的方法都返回true,这个方法才返回true            return checkUser() && checkMail();        }        //验证用户名        function checkUser () {//1. 创建正则表达式            var reg = /^[a‐zA‐Z][a‐zA‐Z0‐9]{3,15}$/;//2. 得到文本框中输入的值            var value = document.getElementById("user").value;//3. 如果不匹配,在后面的span中显示错误信息,返回false            if (reg.test(value)==false) {                document.getElementById("userInfo").innerHTML = "用户名不正确";                return false;            }//4. 如果匹配,在后面的span中显示一个打勾图片,返回true            else {                document.getElementById("userInfo").innerHTML = "<img src='img/gou.png' width='15'/>";                return true;            }        }        //验证邮箱        function checkMail () {            //1. 创建正则表达式            var reg = /^\w+@\w+(\.[a‐zA‐Z]{2,3}){1,2}$/;//2. 得到文本框中输入的值            var value = document.getElementById("email").value;//3. 如果不匹配,在后面的span中显示错误信息,返回false            if (reg.test(value)==false) {                document.getElementById("emailInfo").innerHTML = "邮箱格式不正确";                return false;            }//4. 如果匹配,在后面的span中显示一个打勾图片,返回true            else {                document.getElementById("emailInfo").innerHTML = "<img src='img/gou.png' width='15'/>";                return true;            }        }    </script></head><body><form action="server" method="post" id="myform" onsubmit="return checkAll()">    <table class="main" border="0" cellspacing="0" cellpadding="0">        <tr>            <td><img src="img/logo.jpg" alt="logo" /><img src="img/banner.jpg" alt="banner" /></td>        </tr>        <tr>            <td class="hr_1">新用户注册</td>        </tr>        <tr>            <td style="height:10px;"></td>        </tr>        <tr>            <td>                <table width="100%" border="0" cellspacing="0" cellpadding="0">                    <tr>                        <!‐‐ 长度为4~16个字符,并且以英文字母开头 ‐‐>                        <td class="left">用户名:</td>                        <td class="center">                            <input id="user" name="user" type="text" class="in" onblur="checkUser()"/>                            <span style="color: red" id="userInfo"></span>                        </td>                    </tr>                    <tr>                        <!‐‐ 不能为空, 输入长度大于6个字符 ‐‐>                        <td class="left">密码:</td>                        <td class="center">                            <input id="pwd" name="pwd" type="password" class="in" />                        </td>                    </tr>                    <tr>                        <!‐‐ 不能为空, 与密码相同 ‐‐>                        <td class="left">确认密码:</td>                        <td class="center">                            <input id="repwd" name="repwd" type="password" class="in"/>                        </td>                    </tr>                    <tr>                        <!‐‐ 不能为空, 邮箱格式要正确 ‐‐>                        <td class="left">电子邮箱:</td>                        <td class="center">                            <input id="email" name="email" type="text" class="in" onblur="checkMail()"/>                            <span id="emailInfo" style="color: red;"></span>                        </td>                    </tr>                    <tr>                        <!‐‐ 不能为空, 使用正则表达式自定义校验规则,1开头,11位全是数字 ‐‐>                        <td class="left">手机号码:</td>                        <td class="center">                            <input id="mobile" name="mobile" type="text" class="in"/>                        </td>                    </tr>                    <tr>                        <!‐‐ 不能为空, 要正确的日期格式 ‐‐>                        <td class="left">生日:</td>                        <td class="center">                            <input id="birth" name="birth" type="text" class="in"/>                        </td>                    </tr>                    <tr>                        <td class="left">&nbsp;</td>                        <td class="center">                            <input name="" type="image" src="img/register.jpg" />                        </td>                    </tr>                </table></td>        </tr>    </table></form></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发