重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
简单代码如下:
公司主营业务:网站制作、成都做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出宣化免费做网站回馈大家。
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head
titleJS生成验证码/title
style type="text/css"
.code {
background-image: url(code.jpg);
font-family: Arial;
font-style: italic;
color: Red;
border: 0;
padding: 2px 3px;
letter-spacing: 3px;
font-weight: bolder;
}
.unchanged {
border: 0;
}
/style
script language="javascript" type="text/javascript"
var code; //在全局 定义验证码
function createCode() {
code = "";
var codeLength = 6;//验证码的长度
var checkCode = document.getElementById("checkCode");
var selectChar = 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 = 0; i codeLength; i++) {
var charIndex = Math.floor(Math.random() * 36);
code += selectChar[charIndex];
}
//alert(code);
if (checkCode) {
checkCode.className = "code";
checkCode.value = code;
}
}
function validate() {
var inputCode = document.getElementById("input1").value;
if (inputCode.length = 0) {
alert("请输入验证码!");
} else if (inputCode != code) {
alert("验证码输入错误!");
createCode();//刷新验证码
} else {
alert("^-^ OK");
}
}
/script
/head
body onload="createCode()"
form action="#"
input type="text" id="input1" / input type="text" onclick="createCode()" readonly="readonly" id="checkCode" class="unchanged" style="width: 80px" /br /
input id="Button1" onclick="validate();" type="button" value="确定" /
/form
/body
/html
注意别用在实际生产项目中
我来举个例子,很简单的
!DOCTYPE html
html
head
meta charset="UTF-8"
titleInsert title here/title
script type="text/javascript"
function formCheck(){//表单验证
var userForm=document.forms.userForm;
if(userForm.username.value==null||userForm.username.value.length=0){
alert("用户名不能为空");
return false;
}else if(userForm.username.value.length6||userForm.username.value.length20){
alert("用户名必须为6-20位");
return false;
}else if(userForm.password.value==null||userForm.password.value.length=0){
alert("密码不能为空");
return false;;
}else if(userForm.password.value.length6||userForm.password.value.length20){
alert("密码必须为6-20位");
return false;
}else{
userForm.submit();
}
}
/script
/head
body
form action="" name="userForm"
userName:input type="text" name="username"br
password:input type="password" name="password"br
input type="button" value="提交" onclick=" formCheck()"
/form
/body
/html
我的开发工具是myeclipse复制以下代码,即可生成效果.
html xmlns=" "
head runat="server"
title无标题页/title
/head
body
form id="form1" runat="server"
div
div id="divCode" style="background-color:White; width:52px; height:16px; padding:5px; text-align:center; vertical-align:middle; letter-spacing:5px; border:solid 1px blue"span/spanspan /spanspan /spanspan /span/div
/div
input id="Button1" type="button" value="换一张" onclick="JavaScript:validteCode()"/
/form
/body
/html
script language="JavaScript" type="text/JavaScript"
function validteCode()
{
var codes = new Array(4); //用于存储随机验证码
var colors = new Array("Red","Green","Gray","Blue","Maroon","Aqua","Fuchsia","Lime","Olive","Silver");
for(var i=0;i codes.length;i++)
{//获取随机验证码
codes[i] = Math.floor(Math.random()*10);
}
var spans = document.getElementById("divCode").all;
for(var i=0;ispans.length;i++)
{
spans[i].innerHTML=codes[i];
spans[i].style.color = colors[Math.floor(Math.random()*10)]; //随机设置验证码颜色
}
}
document.onload = validteCode();
/script
用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 = 0; i codeLength; i++) {//循环操作
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("^-^"); //弹出^-^
}
}