用正則表達式判斷一個簡單的用戶登陸的例子
2024-07-21 02:15:21
供稿:網友
 
bd.jsp 
<%@ page contenttype="text/html;charset=gb2312"%> 
<% //接受表單提交的字符串, 
/* 
數據只能為a-z,a-z,0-9的字符的正則表達式的例子. 
正則表達式在做程序登陸的過程非常重要。不然用´ or ´ 等語句就可以很容易繞過您的程度判斷; 
作者:高頌 qq:932246 一個簡單的例子但是很有用,希望起到拋磚引玉的作用。 
*/ 
string temp ="我是非法的字符"; 
string name = request.getparameter("name"); //應收金額 
string pass = request.getparameter("pass"); 
if (name==null){ 
name=temp; 
} 
if (pass==null){ 
pass=temp; 
} 
%> 
<%!private boolean regex(string str){ 
java.util.regex.pattern p=null; //正則表達式 
java.util.regex.matcher m=null; //操作的字符串 
boolean value=true; 
try{ 
p = java.util.regex.pattern.compile("[^0-9a-za-z]"); 
m = p.matcher(str); 
if(m.find()) { 
value=false; 
} 
}catch(exception e){} 
return value; 
} 
%> 
<html> 
<title>信息輸入表單</title> 
<body> 
<form action = "bd.jsp" method = "get" > 
<div align="center"><font color="#ff0000">數據只能為a-z,a-z,0-9的字符的正則表達式的例子</font> 
<table border = "1"> 
<tr> 
<td bgcolor = "yellow">用戶</td> 
<td><input name = "name" type = "text" id="name"></td> 
</tr> 
<tr> 
<td width="293" bgcolor = "yellow">密碼</td> 
<td width="240"> <input name = "pass" type = "text" id="input"></td> 
</tr> 
<tr> 
<td colspan = "2" align = "center"><input name="submit" type = "submit" value = "確定"> 
<input name="reset" type = "reset" value = "取消"> 
</td> 
</tr> 
<tr> 
<td colspan = "2" align = "center">消息提示 
<% 
//判斷: 
if(!regex(name)){ 
out.print("用戶名只能是a-z,a-z,0-9的字符"); 
return;//跳出程序只行 
} 
out.print("恭喜您的數據錄入合法了。<br>"+"您錄入的名字是:"+name+"<br>您錄入的密碼是:"+pass); 
%> 
</td> 
</tr> 
</table> 
</div> 
</form> 
</body> 
</html>