自动生成密码:
function generatepwd()
{
var str = "012345789abcdefghijklmnopqrstuvwxyz";
var len = 6; //随机数的长度
var result = ""; //用于保存结果的变量
for(i=0;i<len;i++){
result += str.charAt(Math.round(Math.random()*(str.length-1)));
}
常见输入验证
function check()
{
var teamName = document.teamForm.teamName.value;
var teamLeaderName = document.teamForm.teamLeaderName.value;
var password = document.teamForm.password.value;
var teamCodeName = document.teamForm.teamCodeName.value;
var instruction = document.teamForm.instruction.value;
var tel =document.teamForm.tel.value;
var email = document.teamForm.email.value;
var applyGrade = document.teamForm.applyGrade.value;
var applyYear = document.teamForm.applyYear.value;
if(teamName=="")//是否为空验证
{
alert('工作组名称不能为空!')
return false;
}
if(teamName!="")//是否允许空格
{
No = " "
for(i=0; i<teamName.length; i++)
{
var Checkstr = teamName.charAt(i);
if (No.indexOf(Checkstr)!= -1)
{
alert("工作组名称不能有空格!");
return false;
}
}
}
if(teamName.length>50)//长度验证
{
alert('工作组名称必须小于50个字符!')
return false;
}
if(teamLeaderName=="")
{
alert('组长姓名不能为空!')
return false;
}
if(teamLeaderName.length>30)
{
alert('组长姓名必须小于30个字符!')
return false;
}
if(teamLeaderName!="")//数字验证
{
No = "0123456789"
for(i=0; i<teamLeaderName.length; i++)
{
var Checkstr = teamLeaderName.charAt(i);
if (No.indexOf(Checkstr)!= -1)
{
alert("组长姓名不应该输入数字!");
return false;
}
}
}
if(password=="")
{
alert('密码不能为空!')
return false;
}
if(password.length<6)
{
alert('密码必须大于6个字符!')
return false;
}
if(password.length>10)
{
alert('密码必须小于10个字符!')
return false;
}
if(teamCodeName.length>100)
{
alert('工作组代号必须小于100个字符!')
return false;
}
if(teamCodeName!="")//英文验证
{
No = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .+-"
for(i=0; i<teamCodeName.length; i++)
{
var Checkstr =teamCodeName.charAt(i);
if (No.indexOf(Checkstr)== -1)
{
alert("工作组代号应该输入英文字符!");
return false;
}
}
}
if(instruction.length>400)
{
alert('简介必须小于400个字符!')
return false;
}
if(tel.length>20)
{
alert('联系电话格式不正确!')
return false;
}
if(tel!="")//电话号码格式验证
{
No = "0123456789()+-"
for(i=0; i<tel.length; i++)
{
var Checkstr = tel.charAt(i);
if (No.indexOf(Checkstr)== -1)
{
alert("联系电话应该输入数字!");
return false;
}
}
}
if(email=="")//Email格式验证
{
alert('联系人Email不能为空!')
return false;
}
if(email.charAt(0)=="."||email.charAt(0)=="@"||email.indexOf('@',0)==-1||email.indexOf('.',0)==-1||email.lastIndexOf("@")==email.length-1||email.lastIndexOf(".")==email.length-1)
{
alert('联系人Email格式不正确!')
return false;
}
if(email.length>30)
{
alert('联系人Email必须小于30个字符!')
return false;
}
else
{
document.teamForm.submit();
alert("恭喜,工作组新增成功!");
}
}
自动生成帐号
public String getNewUserAccount() {
String newAccount = "";
Date date = new Date();//获取当前日期
String year = String.valueOf(date.getYear() + 1900);//年份+1900
String sql = "from Userinfo a where a.userAccount like '" + year + "%'";
List list = userinfoDAO.getSqlresult(sql);
//自动生成帐号
if (list.isEmpty()) {
newAccount = year + "001";
} else {
newAccount = String.valueOf((Integer.parseInt(((Userinfo) list
.get(0)).getUserAccount()) + 1));
}
return newAccount;//返回新帐号
}
注:这种方法是生成如下格式帐号,当年如2009的注册用户依次为2009001,2009002,…..
还可以用其他的帐号生成方式,比如首先获得新注册用户说选择的角色或部门,在后面再加0001,0002等也是可以的。
自动生成验证码
这个资源来源http://kinkding.blog.51cto.com,还有一个Web开发中的验证码图片的生成-基于Java的实现 网址是http://kyo19.javaeye.com/blog/315246,也是自动生成验证码的,可以参考着看。
<%@ page contentType=
"image/jpeg"
import
="java.awt.*,
java.awt.image.*,java.util.*,javax.imageio.*" %>
<%
// 在内存中创建图象
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
// 设定背景色
g.setColor( new Color(0xDCDCDC));
g.fillRect(0, 0, width, height);
// 画边框
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
// 取随机产生的认证码 (4 位数字 )
String rand = request.getParameter( "rand" );
rand = rand.substring(0,rand.indexOf( "." ));
switch (rand.length())
{
case 1: rand = "000" +rand; break ;
case 2: rand = "00" +rand; break ;
case 3: rand = "0" +rand; break ;
default : rand = rand.substring(0,4); break ;
}
// 将认证码存入 SESSION
session.setAttribute( "rand" ,rand);
// 将认证码显示到图象中
g.setColor(Color.black);
Integer tempNumber = new Integer(rand);
String numberStr = tempNumber.toString();
g.setFont( new Font( "Atlantic Inline" ,Font.PLAIN,18));
String Str = numberStr.substring(0,1);
g.drawString(Str,8,17);
Str = numberStr.substring(1,2);
g.drawString(Str,20,15);
Str = numberStr.substring(2,3);
g.drawString(Str,35,18);
Str = numberStr.substring(3,4);
g.drawString(Str,45,15);
// 随机产生 88 个干扰点,使图象中的认证码不易被其它程序探测到
Random random = new Random();
for ( int i=0;i<20;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
g.drawOval(x,y,0,0);
}
// 图象生效
g.dispose();
// 输出图象到页面
ImageIO.write(image, "JPEG" , response.getOutputStream());
%>
java.awt.image.*,java.util.*,javax.imageio.*" %>
<%
// 在内存中创建图象
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
// 设定背景色
g.setColor( new Color(0xDCDCDC));
g.fillRect(0, 0, width, height);
// 画边框
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
// 取随机产生的认证码 (4 位数字 )
String rand = request.getParameter( "rand" );
rand = rand.substring(0,rand.indexOf( "." ));
switch (rand.length())
{
case 1: rand = "000" +rand; break ;
case 2: rand = "00" +rand; break ;
case 3: rand = "0" +rand; break ;
default : rand = rand.substring(0,4); break ;
}
// 将认证码存入 SESSION
session.setAttribute( "rand" ,rand);
// 将认证码显示到图象中
g.setColor(Color.black);
Integer tempNumber = new Integer(rand);
String numberStr = tempNumber.toString();
g.setFont( new Font( "Atlantic Inline" ,Font.PLAIN,18));
String Str = numberStr.substring(0,1);
g.drawString(Str,8,17);
Str = numberStr.substring(1,2);
g.drawString(Str,20,15);
Str = numberStr.substring(2,3);
g.drawString(Str,35,18);
Str = numberStr.substring(3,4);
g.drawString(Str,45,15);
// 随机产生 88 个干扰点,使图象中的认证码不易被其它程序探测到
Random random = new Random();
for ( int i=0;i<20;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
g.drawOval(x,y,0,0);
}
// 图象生效
g.dispose();
// 输出图象到页面
ImageIO.write(image, "JPEG" , response.getOutputStream());
%>
通过
URL
访问测试即可:
[url]http://localhost:8080/test/codetest.jsp?rand=1080.[/url]
本文转自 gaochaojs 51CTO博客,原文链接:http://blog.51cto.com/jncumter/164023,如需转载请自行联系原作者