View Code
1 <script type="text/javascript"> 2 function validateCode() { 3 var validatevalue = ""; 4 var codes = new Array(4); 5 var colors = new Array("Red", "Green", "Gray", "Blue", "Maroon", "Aqua", "Fuchsia", "Lime", "Olive", "Silver"); 6 for (var i = 0; i < codes.length; i++) { 7 codes[i] = Math.floor(Math.random() * 10); 8 validatevalue += codes[i]; 9 } 10 var spans = document.getElementById("divCode").all; 11 for (var i = 0; i < spans.length; i++) { 12 spans[i].innerText = codes[i]; 13 spans[i].style.color = colors[Math.floor(Math.random() * 10)]; 14 } 15 document.getElementById("validatevalue").value = validatevalue; 16 } 17 18 function validate() { 19 if (document.getElementById("textfield3").value != document.getElementById("validatevalue").value) { 20 alert("验证码错误"); 21 validateCode(); 22 document.getElementById("textfield3").value = ""; 23 document.getElementById("textfield3").focus(); 24 return false; 25 } else { 26 return true; 27 } 28 } 29 document.onload = validateCode(); 30 </script>