怎样用JavaScript实现验证码的验证

JavaScript06

怎样用JavaScript实现验证码的验证,第1张

用JavaScript实现验证码的验证主要代码如下:

var code //在全局定义验证码

//产生验证码

window.onload = function createCode(){

code = ""

var codeLength = 4//验证码的长度

var checkCode = document.getElementById("code")

var random = new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',

'S','T','U','V','W','X','Y','Z')//随机数

for(var i = 0i <codeLengthi++) {//循环操作

var index = Math.floor(Math.random()*36)//取得随机数的索引(0~35)

code += random[index]//根据索引取得随机数加到code上

}

checkCode.value = code//把code值赋给验证码

}

//校验验证码

function validate(){

var inputCode = document.getElementById("input").value.toUpperCase()//取得输入的验证码并转化为大写

if(inputCode.length <= 0) { //若输入的验证码长度为0

alert("请输入验证码!")//则弹出请输入验证码

}

else if(inputCode != code ) { //若输入的验证码与产生的验证码不一致时

alert("验证码输入错误!@_@")//则弹出验证码输入错误

createCode()//刷新验证码

document.getElementById("input").value = ""//清空文本框

}

else { //输入正确时

alert("^-^")//弹出^-^

}

}

步骤如下:

1、选中当前工程,右键单击properties,弹出properties界面

2、然后选择MyEclipse—>validation—>Excluded Resource下找到不需要验证的文件或者文件夹

3、在不需要验证的文件或者文件夹前打勾,然后点击 "OK"按钮保存。我的js,jsp,html在webroot下。

4、解决Cannot return from outside a function or method报的错。法一:在MyEclipse菜单栏window --->preferences --->myeclipse --->validation --->javascript validator for Js    files 把Bulid 复选框的勾去掉,就可以了。

5、在所建立的工程项目上右键单击,找到myeclipse-->Exclude Form Validation单击一下,打上√号,js的错误就没了!

扩展资料

eclipse快捷方式

<!--[if !supportLists]-->0. Ctrl + 1 (快速修复)

<!--[if !supportLists]-->1. Ctrl + D (删除当前行)

<!--[if !supportLists]-->2. Ctrl + Alt + ↓(复制当前行到下一行)

<!--[if !supportLists]-->3. Alt + / 或者说是 Ctrl + 空格(由于后者与输入法的快捷键冲突,所以,我一般都用前者) 作用:快速插入。

<!--[if !supportLists]-->4. Alt+Shift+R 重命名非常好用。

<!--[if !supportLists]-->5. Ctrl + Q 定位到最后编辑的地方。

<!--[if !supportLists]-->6. Ctrl + Shift + O 自动导入包。

<!--[if !supportLists]-->7. Ctrl+/ 注释当前行,再按则取消注释。

<!--[if !supportLists]-->8. Ctrl+K快速查找。

<!--[if !supportLists]-->9. Ctrl + Shift + F 自动缩进。

参考资料来源:百度百科:eclipse