正则表达式在JSP中的使用
 <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>京东商城新用户注册</title>

    <style type="text/css">
        .label{
            position:absolute;
            right:70%;
        }
        .fi{
            position:relative;
            left:30%;
        }
        .clr{
            height:20px;
            color:AAAAAA;
            font-size:12px;
            visibility:hidden;
        }
        .s{
            font-weight:lighter;
            color:red;
        }
        .text{
            font-family:宋体;
            width:200px;
        }
        .v{
            color:cccccc;
            font-size:12px;
        }
        a{
            font-size:12px;
        }
        .btn-img{
            position:relative;
            left:30%;
        }
        .ok{
            background:url("image\gou_meitu_1.jpg");
            width:17px;
            height:16px;
            visibility:hidden;
            display:none;
        }
        #strength{
            color:AAAAAA;
            font-size:12px;
            display:none;
        }
        .showStren{
            width:135px;
            display:inline;
            color:white;
            font-size:12px;
        }
        .ceil{
            border:0.5px solid #FFFFFF;
            background-color:DDDDDD;
        }
    </style>

    <script type="text/javascript">
        function inUser(){
            username_mess.style.visibility="visible";
        }
        function outUser(){
            u=f1.username.value;
            f1.username.style.border="1px solid AAAAAA";
            if(u==""){
                username_mess.style.visibility="hidden";
                return;
            }

            reg= /.{4,20}/;             //代表着允许4到20位字符,用两个/表示定义表达式,这样reg就有test方法了,返回值是true和false,另外还有exec方法,返回值是null,意味着不匹配,以及字符串,意味着匹配上了什么字符串
            if(!reg.test(u)){
                username_mess.innerHTML="用户名长度只能在4-20位字符之间";
                username_mess.style.color="red";
                f1.username.style.color="red";
                f1.username.style.border="1px solid red"
                username_ok.style.visibility="hidden";
                return;
            }

            reg= /^[\u4e00-\u9fa5 \w-]{4,20}$/; //代表着中文字符和\w(\w能够代替[a-zA-Z0-9_])还有-
            if(reg.test(u)){
                username_ok.style.visibility="visible";
                username_mess.innerHTML="";
                f1.username.style.color="black";
            }else{
                username_mess.innerHTML="用户名只能由中文、英文、数字及"_"、"-"组成";
                username_mess.style.color="red";
                f1.username.style.color="red";
                f1.username.style.border="1px solid red";
                username_ok.style.visibility="hidden";
            }
        }

        function inPass(){
            pass_mess.style.visibility="visible";
            f1.pwd.style.border="1px solid gold";
        }
        function outPass(){
            p=f1.pwd.value;
            f1.pwd.style.border="1px solid AAAAAA";
            if(p==""){
                pass_mess.style.visibility="hidden";
                return;
            }
        }
        function inRPass(){
            rpass_mess.style.visibility="visible";
            f1.pwd2.style.border="1px solid gold";
        }
        function outRPass(){
            p=f1.pwd2.value;
            f1.pwd2.style.border="1px solid AAAAAA";
            if(p==""){
                rpass_mess.style.visibility="hidden";
                return;
            }
            if(!(f1.pwd.value==f1.pwd2.value)){
                rpass_mess.innerHTML="两次输入的密码不一致";
                rpass_mess.style.color="red";
                f1.pwd2.style.color="red";
                f1.pwd2.style.border="1px solid red";
                repass_ok.style.visibility="hidden";
                return;
            }
        }

        function visibe(){
            if(f1.visi.checked){        //通过checked属性,能够得到这个复选框是否被选中的Boolean值
                pass.style.backgroundColor="FFEEEE";
                v=f1.pwd.value;
                v2=f1.pwd2.value;
                chang.innerHTML= "<input type='text' name='pwd' class='text' tabindex='2' value='"+v+"' onFocus='inPass()' onBlur='outPass()'/>";
                chang2.innerHTML="<input type='text' name='pwd2' class='text' tabindex='2' value='"+v2+"' onFocus='inRPass()' onBlur='outRPass()'/>";
            }else{
                pass.style.backgroundColor="FFFFFF";
                v=f1.pwd.value;
                v2=f1.pwd2.value;
                chang.innerHTML="<input type='password' name='pwd' class='text' tabindex='2' value='"+v+"' onFocus='inPass()' onBlur='outPass()'/>";
                chang2.innerHTML=   "<input type='password' name='pwd2' class='text' tabindex='2' value='"+v2+"' onFocus='inRPass()' onBlur='outRPass()'/>";
            }
        }

        function keyPass(){
            if(f1.pwd.value.length>=6){
                pass_mess.style.display="none";
                strength.style.display="inline";
                sum=checkStrong(f1.pwd.value);
                if(sum==1){
                    l.style.backgroundColor="orange";
                }
                if(sum==2){
                    l.style.backgroundColor="orange";
                    m.style.backgroundColor="orange";
                }
                if(sum==3){
                    l.style.backgroundColor="orange";
                    m.style.backgroundColor="orange";
                    h.style.backgroundColor="orange";
                }
            }
        }

        function checkStrong(Str){
            sum=0;
            reg= /[a-zA-Z]/;
            if(reg.test(Str)){
                sum++;
            }
            reg= /\d/;
            if(reg.test(Str)){
                sum++;
            }
            reg= /[_-]/;
            if(reg.test(Str)){
                sum++;
            }
            return sum;
        }

        function inMail(){
            mail_mess.style.visibility="visible";
            f1.mail.style.border="1px solid gold";
        }
        function outMail(){
            mm=f1.mail.value;
            f1.mail.style.border="1px solid AAAAAA";
            if(mm==""){
                mail_mess.style.visibility="hidden";
                return;
            }
            reg=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
            if(reg.test(mm)){
                mail_ok.style.visibility="visible";
                mail_mess.innerHTML="";
                f1.mail.style.color="black";
            }else{
                mail_mess.innerHTML="邮箱格式不正确";
                f1.mail.style.color="red";
                f1.mail.style.color="red";
                f1.mail.style.border="1px solid red";
                mail_ok.style.visibility="hidden";
            }
        }

    </script>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <!--我们用div显示提示信息的时候,有两种常用的方法,一是事先写好预设的信息到div,将其设置为隐藏,当事件发生的时候,将相应的信息设置成可见。  -->
  <!--第二种方式使直接用innerHTML写信息到div中 -->
  <body>
    <form id="f1">
        <div class="label"><b class="s">*</b>用户名:</div>
        <div class="fi">
            <input type="text" name="username" class="text" tabindex="1" onFocus="inUser()" onBlur="outUser()"/>    <!--动作:光标进入输入框的事件是onFocus,离开输入框的事件是onBlur -->
            &nbsp;&nbsp;<label id="username_ok" class="ok"></label><br/>        <!--label标签不会再新的一行显示  -->
            <div id="username_mess" class="clr">4-20位字符,可由中文、英文、数字及"_"、"-"组成</div>  <!--事先写好预设的信息到div  -->
        </div>

        <div id="pass">
        <div class="label"><b class="s">*</b>设置密码:</div>
        <div class="fi">
            <div id="chang" style="display:inline">
            <input type="password" name="pwd" class="text" tabindex="2" onFocus="inPass()" onBlur="outPass()" onKeyup="keyPass()"/>
            </div>
            &nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" name="visi" id="viewpwd" onClick="visibe()"/>
            <label id="pass_ok" class="ok"></label>
            <label class="v">显示密码字符</label><br/>
            <div id="strength">安全强度:
                <table class="showStren" align="center">
                    <tr align="center">
                        <td id="l" class="ceil">弱</td>
                        <td id="m" class="ceil">中</td>
                        <td id="h" class="ceil">强</td>
                    </tr>
                </table>
            </div>
            <div id="pass_mess" class="clr">6-16位字符,可由英文、数字及"_"、"-"组成</div>
        </div>

        <div class="label"><b class="s">*</b>确认密码:</div>
        <div class="fi">
            <div id="chang2">
            <input type="password" name="pwd2" class="text" tabindex="3" onFocus="inRPass()" onBlur="outRPass()"/>
            </div>
            <label id="rpass_ok" class="ok"></label><br/>
            <div id="rpass_mess" class="clr">请再次输入密码</div>
        </div>
        </div>

        <div class="label"><b class="s">*</b>邮箱:</div>
        <div class="fi">
            <input type="text" name="mail" class="text" tabindex="4" onFocus="inMail()" onBlur="outMail()"/>
            <label id="mail_ok" class="ok"></label>
            &nbsp;&nbsp;&nbsp;&nbsp;<label class="v">免费邮箱:</label>
            <a href="">搜狐</a>
            <a href="">网易</a><br/>
            <div id="mail_mess" class="clr">请输入常用的邮箱,将用来找回密码、接收订单通知等</div>
        </div>

        <div class="label"><b class="s">*</b>推荐人用户名:</div>
        <div class="fi">
            <input type="text" name="referrer" class="text" value="可不填" tabindex="5"/><br/><div class="clr"></div>
        </div>

        <input type="button" name="pwd2" class="btn-img" id="registsubmit" value="同意一下协议,提交" tabindex="8"/>
    </form>
  </body>
</html>

1个回答

你这个正则明显写的有问题:reg= /.{4,20}/;

应该是 reg= /^.{4,20}$/;

u013248631
陌小颜 但是为什么改过来之后还是无法实现我想要的效果呢?下面的一些正则表达式我都已经改过来了
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐