您的位置:首页 > Web前端 > JavaScript

毕业设计(七)---正则表达式通过js表单验证,快速注册流程,简单MD5加密

2013-01-09 20:27 951 查看
一:注册页面 以及 表单验证代码

<script type="text/javascript" src="homepage/js/jquery144.js"></script>
<script type="text/javascript">
function username1() {
var u = $("#username").val();
if (u.length<6||u.length>20) {
$("#name").show();
return false;
}
var regu = "^[0-9a-zA-Z\u4e00-\u9fa5]+$";
var re = new RegExp(regu);
if (!re.test(u)) {
$("#name").show();
return false;
}
$("#name").hide();
return true;
}
function password1() {
var u = $("#password").val();
if (u.length < 6) {
$("#pas").show();
return false;
}
$("#pas").hide();
return true;
}
function repassword1() {
var re = $("#repassword").val();
if (re != $("#password").val()) {
$("#repas").show();
return false;
}
$("#repas").hide();
return true;
}
function email1() {
var mail = $("#email").val();
//存在@符号
var a = mail.indexOf("@");
//是否存在点
var point = mail.indexOf(".");
//存在@,点,并且 点在@之后,且不相邻
if (a == -1 || point == -1 || point - a <= 1) {
$("#ma").show();
//  alert("邮箱格式不正确。正确的例如abc@163.com");
$("#email").focus();
return false;
}
//@不能够是第一个字符,点不能够是最后一个字符
if (a == 0 || point == mail.length - 1) {
$("#ma").show();
return false;
}
$("#ma").hide();
return true;
}
</script>

</head>

<body>
<form action="Register" method="post"  >
姓名:<input type="text" id="username" name="username" onblur="username1();"  />
<div id="name" style="display: none;color:red">
(用户名只能由汉字、字母数字组成,且在6-20位之间)</div>
<br />

密码:<input type="password" id="password" name="password" onblur="password1();" />
<div id="pas" style="display: none;color:red">(请输入超过6位数的密码)</div>
<br />

再次输入密码:<input type="password" id="repassword" name="repassword" onblur="repassword1();" />
<div id="repas" style="display: none;color:red">(确认密码请和输入密码相同)</div>
<br />

email:<input type="text" id="email" name="email" onblur="email1();" />
<div id="ma" style="display: none;color:red">
(邮箱格式不正确。正确的例如abc@163.com)</div>
<br />

性别:男<input type="radio" name="sex" value="male" />  女<input
type="radio" name="sex" value="female" /><br />

<input	type="submit" value="注册" />   <input type="reset" value="重置" />

</form>
</body>


二: 简单流程,
表单的 action属性为 "Register"

<form action="Register" method="post"  >
在struts.xml里对应

<action name="Register" class="ActionUser" >
<result name="success" >/homepage/Homepage.jsp</result>
</action>


对应action 那么为"Register"

class为"ActionUser" 并不是我们原来熟悉的com.xxx.xxx.class 原因是action用注解方式放入spring容器中了,

注解为@Component("ActionUser")

@Component("ActionUser")
@Scope("prototype")
public class ActionUser extends ActionSupport {
private String username;
private String password;
private String email;
private String sex;
....
...
}


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2图说明





-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

三:简单的md5加密算法:

package com.myblog.tool;
import java.security.MessageDigest;

public class MD5 {
private String inStr;
private MessageDigest md5;

/**
* Constructs the MD5 object and sets the string whose MD5 is to be computed.
*
* @param inStr the <code>String</code> whose MD5 is to be computed
*/
public MD5(String inStr)
{
this.inStr = inStr;
try
{
this.md5 = MessageDigest.getInstance("MD5");
}
catch (Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
}
}

/**
* Computes the MD5 fingerprint of a string.
*
* @return the MD5 digest of the input <code>String</code>
*/
public String compute()
{
// convert input String to a char[]
// convert that char[] to byte[]
// get the md5 digest as byte[]
// bit-wise AND that byte[] with 0xff
// prepend "0" to the output StringBuffer to make sure that we don't end up with
// something like "e21ff" instead of "e201ff"

char[] charArray = this.inStr.toCharArray();

byte[] byteArray = new byte[charArray.length];

for (int i=0; i<charArray.length; i++)
byteArray[i] = (byte) charArray[i];

byte[] md5Bytes = this.md5.digest(byteArray);

StringBuffer hexValue = new StringBuffer();

for (int i=0; i<md5Bytes.length; i++)
{
int val = ((int) md5Bytes[i] ) & 0xff;
if (val < 16) hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}

return hexValue.toString();
}

public static void main(String[] args)
{
MD5 md5=new MD5("abc");
String postString = md5.compute();
System.out.println(postString);
if(postString.equals("900150983cd24fb0d6963f7d28e17f72"))
{
System.out.println("true");
}
else
System.out.println("false");
}
}


在保存密码的时候把密码加密,这样在数据库中也看不到明文.

在登录时候,先把密码加密,再和数据库中的比较.











                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: