
本分分享一个简单的jQuery验证码生成刷新与验证
主要js
<script> // 验证码 // 验证码刷新 function code() { var str = "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM"; var str1 = 0; for (var i = 0; i < 4; i++) { str1 += str.charAt(Math.floor(Math.random() * 62)) } str1 = str1.substring(1) $("#code").text(str1); } code(); $("#code").click(code); // 验证码验证 $('input').eq(0).blur(function () { if ($(this).val().length == 0) { $(this).parent().next().next("div").text(""); $(this).parent().next().next("div").css("color", '#ccc'); } else if ($(this).val().toUpperCase() != $("#code").text().toUpperCase()) { $(this).parent().next().next("div").text("验证码不正确"); $(this).parent().next().next("div").css("color", 'red'); } else { $(this).parent().next().next("div").text("验证码正确"); $(this).parent().next().next("div").css("color", 'green'); } }) </script>