JSP判斷用戶名是否符合規則:用戶名只能由字母數字下劃線組成,並且首字母必須爲字母

用戶名只能由字母數字下劃線組成,並且首字母必須爲字母
本程序使用jsp中的javabean判斷是否符合用戶名規則

index.jsp

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="checkisvailued.jsp" method="post">
	用戶名:<input type="text" name="username"><br>
	<p style="font-size:20px;color:red">注意:用戶名只能由字母數字下劃線組成,並且首字母必須爲字母</p>
	<input type="submit" value="提交">
</form>
</body>
</html>

checkisvailued.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page language="java" import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<%
	request.setCharacterEncoding("UTF-8");
%>
<body>
<%
	String name=request.getParameter("username");
%>
<jsp:useBean id="strBean" class="com.isVaild"></jsp:useBean>
<jsp:setProperty property="str" name="strBean" value="<%=name %>"/>
<p> 您輸入的用戶名是:<jsp:getProperty  property="str" name="strBean"/></p>
<p> 是否有效:<jsp:getProperty  property="vaild" name="strBean"/></p>
<p> 提示信息爲:<jsp:getProperty  property="cue" name="strBean"/></p>
</body>
</html>

isValid.java

package com;
public class isVaild {
	private String str;
	private boolean vaild;
	private String cue;
	public String getStr() {
		return str;
	}
	public void setStr(String str) {
		this.str=str;
	}
	public String getCue() {
		return cue;
	}
	public void setCue(String cue) {
		this.cue=cue;
	}
	public boolean isVaild() {
		char cArr[]=str.toCharArray();
		int firstChar=(int)cArr[0];
		if((firstChar>=65&&firstChar<=90)||(firstChar>=97&&firstChar<=122)) {
			for(int i=1;i<cArr.length;i++) {
				int ascii=cArr[i];
				if(!((ascii>=48&&ascii<=57)||(ascii>=65&&ascii<=90)||
				(ascii>=97&&ascii<=122)||(ascii==95))) {
					this.setCue("用戶名格式錯誤,用戶名只能由字母數字下劃線組成");
					this.vaild=false;
					return vaild;
				}
			}
			this.setCue("用戶名格式正確");
			this.vaild=true;
			return vaild;
		}else {
			this.setCue("用戶名格式錯誤,首字符必須爲字母");
			this.vaild=false;
			return vaild;
		}
	}
}

運行截圖:

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章