2、javascript+CSS+Html实现用户注册及登录的格式验证。在用户登录功能中试加入图片验证码功能

html-css014

2、javascript+CSS+Html实现用户注册及登录的格式验证。在用户登录功能中试加入图片验证码功能,第1张

下面是关键代码,如果剩下的你都搞不懂,我就无语了

JS

<script type="text/javascript" language="javascript">

function reloadcodeOne(){//刷新验证码函数

var verify = document.getElementById('checkCodeImg')

verify.setAttribute('src', 'validateCode?dt=' + Math.random())

}

<script type="text/javascript" >

html

<p>

<label>验证码:</label>

<input class="code" value="请输入验证码" title="请输入验证码" name="rendCode" id="rendCode" onfocus="if (value =='请输入验证码'){value =''}" onblur="if (value ==''){value='请输入验证码'}" type="text" size="6" />

<span><img id="checkCodeImg" src="validateCodeServlet" onclick="javascript:reloadcodeOne()" alt="" width="75" height="24" /></span>

</p>

java代码

package com.zhihui.action.common

import java.awt.Color

import java.awt.Font

import java.awt.Graphics2D

import java.awt.image.BufferedImage

import java.util.Random

import javax.imageio.ImageIO

import javax.servlet.ServletException

import javax.servlet.ServletOutputStream

import javax.servlet.http.HttpServletRequest

import javax.servlet.http.HttpServletResponse

import javax.servlet.http.HttpSession

import org.apache.struts2.ServletActionContext

import com.zhihui.action.base.BaseAction

/**

* <p>

* 校验码控制器

* </p>

*

* @author liurong

* @version ValidateCodeServlet.java,v 0.1 2008-11-20 上午09:22:31 Administrator

* Exp

*/

public class ValidateCodeAction extends BaseAction {

private static final long serialVersionUID = 1L

// 验证码图片的宽度。

private int width = 10

// 验证码图片的高度。

private int height = 5

// 验证码字符个数

private int codeCount = 5

private int x = 0

// 字体高度

private int fontHeight

private int codeY

/*char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J',

'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'U', 'V', 'W',

'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' }*/

char[] codeSequence = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0' }

HttpServletRequest req=ServletActionContext.getRequest()

HttpServletResponse resp=ServletActionContext.getResponse()

public String execute()

throws ServletException, java.io.IOException {

// 宽度

String strWidth = "70"

// 高度

String strHeight = "22"

// 字符个数

String strCodeCount = "5"

width = Integer.parseInt(strWidth)

height = Integer.parseInt(strHeight)

codeCount = Integer.parseInt(strCodeCount)

x = width / (codeCount)

fontHeight = height - 4

codeY = height - 4

// 定义图像buffer

BufferedImage buffImg = new BufferedImage(width, height,

BufferedImage.TYPE_INT_RGB)

Graphics2D g = buffImg.createGraphics()

Random random = new Random()

// 将图像填充为白色

g.setColor(Color.WHITE)

g.fillRect(0, 0, width, height)

Font font = new Font("Fixedsys", Font.PLAIN, fontHeight)

g.setFont(font)

g.setColor(Color.BLACK)

g.drawRect(0, 0, width - 1, height - 1)

g.setColor(Color.BLACK)

for (int i = 0i <15i++) {

int x = random.nextInt(width)

int y = random.nextInt(height)

int xl = random.nextInt(8)

int yl = random.nextInt(8)

g.drawLine(x, y, x + xl, y + yl)

}

// randomCode用于保存随机产生的验证码,以便用户登录后进行验证。

StringBuffer randomCode = new StringBuffer()

int red = 0, green = 0, blue = 0

for (int i = 0i <codeCounti++) {

String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)])

red = 0//random.nextInt(255)

green = 0//random.nextInt(255)

blue = 0//random.nextInt(255)

g.setColor(new Color(red, green, blue))

g.drawString(strRand, (i ) * x, codeY)

randomCode.append(strRand)

}

HttpSession session = req.getSession()

session.setAttribute("validateCode", randomCode.toString())

resp.setHeader("Pragma", "no-cache")

resp.setHeader("Cache-Control", "no-cache")

resp.setDateHeader("Expires", 0)

resp.setContentType("image/jpeg")

ServletOutputStream sos = resp.getOutputStream()

ImageIO.write(buffImg, "jpeg", sos)

sos.close()

return null

}

public int getWidth() {

return width

}

public void setWidth(int width) {

this.width = width

}

public int getHeight() {

return height

}

public void setHeight(int height) {

this.height = height

}

public int getCodeCount() {

return codeCount

}

public void setCodeCount(int codeCount) {

this.codeCount = codeCount

}

public int getX() {

return x

}

public void setX(int x) {

this.x = x

}

public int getFontHeight() {

return fontHeight

}

public void setFontHeight(int fontHeight) {

this.fontHeight = fontHeight

}

public int getCodeY() {

return codeY

}

public void setCodeY(int codeY) {

this.codeY = codeY

}

public char[] getCodeSequence() {

return codeSequence

}

public void setCodeSequence(char[] codeSequence) {

this.codeSequence = codeSequence

}

public HttpServletRequest getReq() {

return req

}

public void setReq(HttpServletRequest req) {

this.req = req

}

public HttpServletResponse getResp() {

return resp

}

public void setResp(HttpServletResponse resp) {

this.resp = resp

}

}

1)email:邮件输入域,在表达提交时提供的邮箱格式验证,并弹出一个提示窗口。

2)url:地址输入域,在表单提交时提供简单的URL地址格式验证,并弹出一个提示窗口。

3)number:数字输入域,(可设置min、max、step)。

4)tel:电话号码输入域,在手机浏览器弹出数字输入域。

5)search:搜索输入域,在手机浏览器右下角呈现搜索按键。

6)range:范围选择空件。

7)color:颜色选择控件。

8)date/month/week:时间选择控件。

1)autocomplete: autocomplete属性规定输入的字段是否应该启用自动完成功能。

自动完成功能允许浏览器预测 字段输入,当用户在字段开始键入时,浏览器基于键入的值。应该显示出在字段中填写的选项。

2)placeholder:占位符,用于在输入框中显示提示性文字,与value不同,不能被提交。

3)autofoaus:自动获取输入焦点。

4)multiple:是否允许多个输入值,若声明该属性,那么输入框允许输入多个用逗号隔开的值。

5)form:值为某个表单的id,若设置,则该输入域可放在该表单外面。

6)required:在表单提交时会验证是否有输入,没有则弹出提示信息。

7)maxlength:限制最大长度,只有在有输入的情况下才有用,不区分中英文。

minlength:限制最小长度,但它不是H5标准属性,仅部分浏览器支持。

8)min:限定输入数字的最小值。

9)max:限定输入数字的最大值。

10)step:限定输入数字的步长,与min连用。

11)pattern:指定一个正则表达式,对输入进行验证。(正则默认首尾加^$)

作用:getCurrentPosition() 方法来获得用户的位置。

格式:一般在js中书写方式。

案例:

DTD 是一套关于标记符的语法规则。它是XML1.0版规格得一部分,是html文件的验证机制,属于html文件组成的一部分。

DTD 是一种保证html文档格式正确的有效方法,可以通过比较html文档和DTD文件来看文档是否符合规范,元素和标签使用是否正确。一个DTD文档包含:元素的定义规则,元素间关系的定义规则,元素可使用的属性,可使用的实体或符号规则。

XML文件提供应用程序一个数据交换的格式,DTD正是让html文件能够成为数据交换的标准,因为不同的公司只需定义好标准的DTD,各公司都能够依照DTD建立html文件,并且进行验证,如此就可以轻易的建立标准和交换数据,这样满足了网络共享和数据交互。

DTD文件是一个ASCII的文本文件,后缀名为.dtd。