JSP实现表单验证代码教程,jsp表单代码教程分享


JSP实现表单验证

第一个体现jsp价值的作业,然而在经历重重问题考验后,Ending

注册表单:register

 <%--   Created by IntelliJ IDEA.   User: Luminary   Date: 2017/10/20   Time: 15:23   To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head>     <title>Register</title>     <style type="text/css">         ul         {             list-style: none;             margin: 80px auto;         }         li         {             margin: 20px;         }     </style> <body> <form action="Post.jsp" onsubmit="return check();" method="post">     <ul> </head>             <li>                 用户名:&nbsp;<input type="text" name="username" id="name" size="25" style="margin-left: 12px;">             </li>             <li>                 密&nbsp;&nbsp;&nbsp;码:&nbsp;<input type="password" name="password" size="25"style="margin-left: 12px;">             </li>             <li>籍&nbsp;&nbsp;&nbsp;贯:                 <select name="home" style="margin-left: 12px;">                     <option selected value="杭州">杭州</option>                     <option value="上海">上海</option>                     <option value="广州">广州</option>                     <option value="北京">北京</option>                     <option value="深圳">深圳</option>                 </select>             </li>             <li>                 出生年月:<input type="text" name="data" size="25">             </li>             <li>                 性&nbsp;&nbsp;&nbsp;别:&nbsp;                 <input type="radio" name="sex" value="男">男&nbsp;&nbsp;                 <input type="radio" name="sex" value="女">女&nbsp;&nbsp;             </li>             <li>                 爱&nbsp;&nbsp;&nbsp;好:&nbsp;                 <input type="checkbox" name="habit" value="Book">Book                 <input type="checkbox" name="habit" value="Running">Running                 <input type="checkbox" name="habit" value="Singing">Singing                 <input type="checkbox" name="habit" value="Dancing">Dancing                 <input type="checkbox" name="habit" value="Game">Game             </li>             <li>                 身&nbsp;&nbsp;&nbsp;高:&nbsp;                 <input type="text" name="height" size="25"> cm             </li>             <li>                 邮&nbsp;&nbsp;&nbsp;箱:&nbsp;                 <input type="text" name="email" id="mail" size="25">             </li>             <li>                 手&nbsp;&nbsp;&nbsp;机:&nbsp;                 <input type="text" name="phone" id="phone" size="25">             </li>             <li>                 简&nbsp;&nbsp;&nbsp;介:&nbsp;                 <textarea maxlength="15" rows="10" cols="25" name="introduce"></textarea>             </li>             <li>                 <input type="reset" value="清空">                 <input type="submit" value="提交">             </li>         </ul>     </form> <script>     function check()     {         //用户名验证         var n=document.getElementById("name");         n = n.value;         if(n.length<6||n.length>10)         {             alert("用户名长度必须在6到10位之间!");             return false;         }         if(n.charAt(0)<'A'||n.charAt(0)>'z')         {             alert("用户名必须以首字母开头");             return false;         }         //邮箱验证         var m=document.getElementById("mail");         m=m.value;         if(m.indexOf("@") < 0 )         {             alert("邮箱中没有包含@字符!");             return false;         }         //手机验证         var p = document.getElementById("phone");         p=p.value;         if(p.length!=11)         {             alert("手机号必须为11位!");             return false;         }     } //    console.log(document.getElementsByTagName("form")); //    document.getElementsByTagName("form")[0].onsubmit =check(); </script> </body> </html>

提交页面:Post.jsp

 <%--   Created by IntelliJ IDEA.   User: Luminary   Date: 2017/10/20   Time: 16:38   To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=gb2312" language="java" errorPage="error.jsp" %> <html> <head>     <title>Title</title>     <style>         ul         {             list-style: none;             margin: 50px;         }         li         {             margin: 10px;         }     </style> </head> <body>     <%request.setCharacterEncoding("UTF-8");%>     <%         String username = request.getParameter("username");         String password = request.getParameter("password");         String home = request.getParameter("home");         String data = request.getParameter("data");         String sex = request.getParameter("sex");         String[] habit = request.getParameterValues("habit");         String height = request.getParameter("height");         String email = request.getParameter("email");         String phone = request.getParameter("phone");         String introduce = request.getParameter("introduce");     %>     <ul>         <li>             用户名:<%=username%>         </li>         <li>             密码:<%=password%>         </li>         <li>             籍贯:<%=home%>         </li>         <li>             出生年月:<%=data%>         </li>         <li>             性别:<%=sex%>         </li>         <li>             爱好:                 <%                     if(habit.length==0)                         throw new Exception( "您没有选择任何爱好!" );                     for(int i=0;i<habit.length;i++)                     {out.println(habit[i]);}                 %>         </li>         <li>             身高:             <%=height%>         </li>         <li>             邮箱:             <%=email%>         </li>         <li>             手机:             <%=phone%>         </li>         <li>             简介:             <%=introduce%>         </li>     </ul> </body> </html>

错误页面:error.jsp

 <%--   Created by IntelliJ IDEA.   User: Luminary   Date: 2017/10/20   Time: 17:52   To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java"  isErrorPage="true" %> <html> <head>     <title>Title</title> </head> <body>     当前页面是:error.jsp<br>     <%out.print("您没有选择任何爱好,请您返回重新选择爱好,至少选择一个!");%> </body> </html>

实现效果

1.没有选择爱好,跳转到错误页面

—-想了解更多的jsp相关干货教程关注<计算机技术网(www.ctvol.com)!!>

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/jspttutorial/111726.html

(0)
上一篇 2020年5月8日
下一篇 2020年5月8日

精彩推荐