给大家分享一个简单的js验证码生成代码
传入元素 如productionVerificationCode(#\(("a")) 反回验证码的结果,#\)("a")元素写入验证码PS:该代码依赖Jquery1.4版本以上
//----[生成数字加减乘法验证码](传入写入元素,返回验证码计算结果)function productionVerificationCode(element) {var code = 9999;var ranColor = '#' + ('00000' + (Math.random() * 0x1000000 << 0).toString(16)).slice(-6); //随机生成颜色// alert(ranColor)var ranColor2 = '#' + ('00000' + (Math.random() * 0x1000000 << 0).toString(16)).slice(-6);var num1 = Math.floor(Math.random() * 100);var num2 = Math.floor(Math.random() * 100);//随机算法var tmparith = Math.floor(Math.random() * 3);var $html = "";switch(tmparith) {case 1:code = num1 + num2;$html = num1 + ' + ' + num2 + ' = ?';break;case 2:if(parseInt(num1) < parseInt(num2)) {var tmpnum = num1;num1 = num2;num2 = tmpnum;}code = num1 - num2;$html = num1 + ' - ' + num2 + ' = ?';break;default:code = num1 * num2;$html = num1 + ' × ' + num2 + ' = ?';break;}element.val($html); //写入验证码if(element.hasClass("nocode")) {element.removeClass("nocode");element.addClass("code");}element.css('background', ranColor);element.css('color', ranColor2);return code;}//----[END][生成数字加减乘法验证码]