使用 jQuery 验证密码一致性,正则表达式验证邮箱及手机号的方法

jQuery 脚本

<script type="text/javascript">
        function check_password() {
            if ($("#password").val() != $("#checkPWD").val()){
                alert("请保证两次输入密码的一致性!");
            }
        }

        function check_email() {
            var reg = /\w+[@]{1}\w+[.]\w+/;
            if (!reg.test($("#email")).val()){
                alert("请输入正确的email!")
            }
        }

        function check_phone() {
            var reg = /^1[34578]\d{9}$/;
            if (!reg.test($("#phone")).val()){
                alert("请输入正确的手机号!")
            }
        }

    </script>

html 文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" +
request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <script src="jquery-3.2.1.js"></script>
    <base href="<%=basePath%>">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>注册界面</title>
</head>
<body>
    <center>
        <h1>用户注册</h1>
        <form action="<%=basePath%>/RegistServlet" method="post">
            <table width="400px" cellspacing="0px" cellpadding="0px" border="1px">
                <tr>
                    <td>用户名</td>
                    <td><input type="text" name="username" placeholder="用户名为3-12位字母数字或下划线组合" ></td>
                </tr>
                <tr>
                    <td>密&nbsp;码</td>
                    <td><input type="password" name="password" placeholder="密码长度为6-12位的纯数字"  id="password"></td>
                </tr>
                <tr>
                    <td>确认密码</td>
                    <td><input type="password" name="checkPWD" placeholder="密码长度为6-12位的纯数字" id="checkPWD" onchange="check_password()"></td>
                </tr>
                <tr>
                    <td>手机号码</td>
                    <td><input type="text" name="phone" placeholder="请输入正确的手机号码格式" id="phone" onchange="check_phone()"></td>
                </tr>
                <tr>
                    <td>邮箱</td>
                    <td><input type="email" name="email" placeholder="请输入正确邮箱格式" id="email" onchange="check_email()" required="required"></td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align:center">
                        <input type="submit" value="注册">
                        <input type="reset" value="重置">
                    </td>
                </tr>
            </table>
        </form>
    </center>

</body>
</html>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值