JSP表單建立及判斷
阿新 • • 發佈:2018-11-04
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8” errorPage=“Enter02.jsp”%>
<%@ page import=“java.util.Random”%>
<%!
//生成隨機數字+字母組合
public static String getRandom(int length) {
String val = “”;
Random random = new Random();
for (int i = 0; i < length; i++) {
// 輸出字母還是數字
String charOrNum = random.nextInt(2) % 2 == 0 ? “char” : “num”;
// 字串
if (“char”.equalsIgnoreCase(charOrNum)) {
// 取得大寫字母還是小寫字母
int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
val += (char) (choice + random.nextInt(26));
} else if (“num”.equalsIgnoreCase(charOrNum)) { // 數字
val += String.valueOf(random.nextInt(10));
}
}
return val;
}%>
<style type="text/css">
.nop{
color:cornflowerblue
}
.nops{
color:red
}
.nopsnd{
color:cornflowerblue
}
</style>
<form action="long.jsp"> 姓名<input type="text" name="useid" value="<%=request.getParameter("useid") == null ? "" : request.getParameter("useid")%>"> <% if(request.getAttribute("useid") == null){ %><span class="nop">最高5位數</span><% }else{ %><span class="nops"><%=request.getAttribute("useid")%></span><% } %> <p> 興趣愛好 <input type="checkbox" name="cb" value="0">泡妹 <input type="checkbox" name="cb" value="1">音樂 <input type="checkbox" name="cb" value="2">睡覺 <% if(request.getAttribute("cb") == null){ %><span class="nop">請選擇至少一個</span><% }else{ %><span class="nops"><%=request.getAttribute("cb") %></span><% } %> <p> 身份證號<input type="text" name="sheid" value="<%=request.getParameter("sheid")==null? "" :request.getParameter("sheid") %>"> <% String sheid = request.getParameter("sheid"); if(request.getAttribute("sheid") == null){ %><span class="nop">請輸入18數字</span><% }else{ %><span class="nops"><%=request.getAttribute("sheid")%></span><% } %> <p> 郵箱<input type="text" name="youid" value="<%=request.getParameter("youid")==null ? "":request.getParameter("youid")%>"> <% String youid = request.getParameter("youid"); if(request.getAttribute("youid") == null){ %><span class="nop">請以@.開頭</span><% }else{ %><span class="nops"><%=request.getAttribute("youid")%></span><% } %> <p> <input type="radio" name="sex" value="1" checked>男 <input type="radio" name="sex" value="2">女 <% String sex = request.getParameter("sex"); if(request.getAttribute("sex") == null){ %><span class="nop">請選擇男 女</span><% }else{ %><span class="nops"><%=request.getAttribute("sex") %></span><% } %> <% String mots=getRandom(4);%> <p>請輸入驗證碼<input type="text" name="yingid" value="<%=request.getParameter("yingid")==null ? "" :request.getParameter("yingid") %>"> <% if(request.getAttribute("equal")==null){ %> <span class="nops"></span> <% }else{ %> <span class="nops"><%=request.getAttribute("equal") %></span> <% } %> <p>驗證碼: <%=mots%> <input type="hidden" value="<%=mots%>" name="mots"> <p><input type="submit" value="提交"> </form>
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%
String useid = request.getParameter("useid");
String[] cb = request.getParameterValues("cb");
String sheid = request.getParameter("sheid");
String youid = request.getParameter("youid");
String sex = request.getParameter("sex");
String index = request.getParameter("yingid");
String ssss = request.getParameter("mots");
System.out.print(ssss +" "+index);
String re = "[a-zA-Z0-9_-] [email protected]([a-zA-Z0-9_-])+([.][a-zA-Z0-9_-]{2,3}){1,2}";
if(useid.length() < 5 && cb != null && sheid.length() == 18 && useid.length() !=0 && youid.matches(re)&&ssss.equals(index)){
%>
<p>你的名字 :<%=useid %>
<p>你的愛好:
<%
for(int i=0;i < cb.length;i++){
if(cb[i].equals("0")){
%>泡妹<%
}else if (cb[i].equals("1")){
%>音樂<%
}else if (cb[i].equals("2")){
%>睡覺<%
}
}
%>
<p>身份證號碼:<%=sheid %>
<p>郵箱:<%=youid %>
<p>性別:<%=sex %>
<%
}else{
String useids;
useids=(useid.length() >0 && useid.length() <5)?"已通過":"你輸入的字元為大於5個字元";
request.setAttribute("useid",useids);
String cbs;
cbs=(cb==null)?"請至少選擇一個愛好 ":"已通過 ";
request.setAttribute("cb",cbs);
String sheids;
sheids=(sheid.length()==18)?"已通過 ":"身份證錯誤 ";
request.setAttribute("sheid",sheids);
String youids;
youids=(youid.matches(re))==true?"已通過 ":"郵箱錯誤 ";
request.setAttribute("youid",youids);
request.setAttribute("sex","已通過 ");
String equal= ssss.equals(index) ? "已通過 ":"驗證碼錯誤 請重新輸入";
request.setAttribute("equal", equal);
request.getRequestDispatcher("form.jsp").forward(request, response);
}
%>