簡單代碼如下:
公司主營業(yè)務(wù):網(wǎng)站制作、成都做網(wǎng)站、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出宣化免費(fèi)做網(wǎng)站回饋大家。
!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');//所有候選組成驗證碼的字符,當(dāng)然也可以用中文的
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
注意別用在實(shí)際生產(chǎn)項目中
我來舉個例子,很簡單的
!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
我的開發(fā)工具是myeclipse復(fù)制以下代碼,即可生成效果.
html xmlns=" "
head runat="server"
title無標(biāo)題頁/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); //用于存儲隨機(jī)驗證碼
var colors = new Array("Red","Green","Gray","Blue","Maroon","Aqua","Fuchsia","Lime","Olive","Silver");
for(var i=0;i codes.length;i++)
{//獲取隨機(jī)驗證碼
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)]; //隨機(jī)設(shè)置驗證碼顏色
}
}
document.onload = validteCode();
/script
用JavaScript實(shí)現(xiàn)驗證碼的驗證主要代碼如下:
var code ; //在全局定義驗證碼
//產(chǎn)生驗證碼
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');//隨機(jī)數(shù)
for(var i = 0; i codeLength; i++) {//循環(huán)操作
var index = Math.floor(Math.random()*36);//取得隨機(jī)數(shù)的索引(0~35)
code += random[index];//根據(jù)索引取得隨機(jī)數(shù)加到code上
}
checkCode.value = code;//把code值賦給驗證碼
}
//校驗驗證碼
function validate(){
var inputCode = document.getElementById("input").value.toUpperCase(); //取得輸入的驗證碼并轉(zhuǎn)化為大寫
if(inputCode.length = 0) { //若輸入的驗證碼長度為0
alert("請輸入驗證碼!"); //則彈出請輸入驗證碼
}
else if(inputCode != code ) { //若輸入的驗證碼與產(chǎn)生的驗證碼不一致時
alert("驗證碼輸入錯誤!@_@"); //則彈出驗證碼輸入錯誤
createCode();//刷新驗證碼
document.getElementById("input").value = "";//清空文本框
}
else { //輸入正確時
alert("^-^"); //彈出^-^
}
}