JavaScript中的验证码不起作用
我有一个使用Javascript的验证表单。它必须验证名称、电子邮件和验证码。但是通过我的验证功能,姓名和电子邮件会被验证,但在提交按钮的JavaScript中的验证码不起作用,javascript,html,validation,Javascript,Html,Validation,我有一个使用Javascript的验证表单。它必须验证名称、电子邮件和验证码。但是通过我的验证功能,姓名和电子邮件会被验证,但在提交按钮的一次点击后,验证码不会被验证 以下函数显示了我的代码: <script type="text/javascript"> function validateform(){ var cm_name=document.supportus_form.cm_name.value; var cm_email=document.supportus_fo
一次点击后,验证码不会被验证
以下函数显示了我的代码:
<script type="text/javascript">
function validateform(){
var cm_name=document.supportus_form.cm_name.value;
var cm_email=document.supportus_form.cm_email.value;
var atpos = cm_email.indexOf("@");
var dotpos = cm_email.lastIndexOf(".");
if (cm_name==null || cm_name=="")
{
alert("Name can't be blank");
return false;
}
else if(atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;
}
var why = "";
if(theform.CaptchaInput.value == ""){
why += "- Please Enter CAPTCHA Code.\n";
}
if(theform.CaptchaInput.value != ""){
if(ValidCaptcha(theform.CaptchaInput.value) == false){
why += "- The CAPTCHA Code Does Not Match.\n";
}
}
if(why != ""){
alert(why);
return false;
}
}
var a = Math.ceil(Math.random() * 9)+ '';
var b = Math.ceil(Math.random() * 9)+ '';
var c = Math.ceil(Math.random() * 9)+ '';
var d = Math.ceil(Math.random() * 9)+ '';
var e = Math.ceil(Math.random() * 9)+ '';
var code = a + b + c + d + e;
document.getElementById("txtCaptcha").value = code;
document.getElementById("CaptchaDiv").innerHTML = code;
// Validate input against the generated number
function ValidCaptcha(){
var str1 = removeSpaces(document.getElementById('txtCaptcha').value);
var str2 = removeSpaces(document.getElementById('CaptchaInput').value);
if (str1 == str2){
return true;
}else{
return false;
}
} </script>
函数validateform(){
var cm_name=document.supportus_form.cm_name.value;
var cm_email=document.supportus_form.cm_email.value;
var atpos=cm_email.indexOf(“@”);
var dotpos=cm_email.lastIndexOf(“.”);
如果(cm_name==null | | cm_name===“”)
{
警报(“名称不能为空”);
返回false;
}
否则,如果(atpos嘿,您以不正确的方式使用了条件标记。这会导致未知的错误。下面的Javascript代码可以纠正您的错误
function validateform(){
var cm_name=document.supportus_form.cm_name.value;
var cm_email=document.supportus_form.cm_email.value;
var atpos = cm_email.indexOf("@");
var dotpos = cm_email.lastIndexOf(".");
var captha = document.getElementById("txtCaptcha").value;
var capthaValue = document.getElementById('CaptchaInput').value.replace(/ /g,'');
console.log(typeof captha);
if(capthaValue == "" || captha != capthaValue) {
validCaptcha()
}
if (cm_name==null || cm_name=="")
{
alert("Name can't be blank");
return false;
}
else if(atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;
}
function validCaptcha() {
var why = "";
if(capthaValue == ""){
why += "- Please Enter CAPTCHA Code.\n";
}
if(capthaValue != ""){
if((captha != capthaValue) == false){
why += "- The CAPTCHA Code Does Not Match.\n";
}
}
if(why != ""){
alert(why);
return false;
}
}
}
var a = Math.ceil(Math.random() * 9)+ '';
var b = Math.ceil(Math.random() * 9)+ '';
var c = Math.ceil(Math.random() * 9)+ '';
var d = Math.ceil(Math.random() * 9)+ '';
var e = Math.ceil(Math.random() * 9)+ '';
var code = a + b + c + d + e;
document.getElementById("txtCaptcha").value = code;
document.getElementById("CaptchaDiv").innerHTML = code;
函数validateform(){
var cm_name=document.supportus_form.cm_name.value;
var cm_email=document.supportus_form.cm_email.value;
var atpos=cm_email.indexOf(“@”);
var dotpos=cm_email.lastIndexOf(“.”);
var captha=document.getElementById(“txtCaptcha”).value;
var capthaValue=document.getElementById('CaptchaInput').value.replace(//g',);
控制台日志(captha类型);
if(capthaValue==“”| | captha!=capthaValue){
validCaptcha()
}
如果(cm_name==null | | cm_name===“”)
{
警报(“名称不能为空”);
返回false;
}
如果是,你是否意识到任何人(包括垃圾邮件机器人)可以通过查看页面源并查看txtCaptcha隐藏字段中的内容来欺骗验证码吗?更好的解决方案可能是使用已建立的验证码解决方案,例如Google的ReCaptcha,它在远程服务器上安全地进行验证。@ADyson,请指导我这样做。如何使用Google的ReCaptcha?我对此不熟悉…你可以在他们的网站上遵循指南:是一般介绍,包含开发人员指南。它是免费使用的。如果你实现了它,但不能在这里用你的代码将其作为一个问题来使用。@ADyson,我遵循了它,我将Google的ReCaptcha保留在我的表单中,这会容易得多。谢谢你从骗子那里救了我。哦,你是个骗子有点晚了,但我将如何使用上面的代码进行测试。@VijayKumarB这可能会修复代码,但我想提醒未来的读者,它不会改变设计存在根本性缺陷的事实。这是因为验证码的答案包含在页面的源代码中,使其不安全,结果无法真实此外,任何完全的客户端验证都是不可信的,因为恶意用户可以简单地更改代码(使用开发工具)以绕过检查,而bot可以完全绕过脚本并直接提交表单。所有数据的服务器端验证应始终按照well@Keynes好的
<style>
.capbox {
background-color: #92D433;
border: #B3E272 0px solid;
border-width: 0px 12px 0px 0px;
display: inline-block;
*display: inline; zoom: 1; /* FOR IE7-8 */
padding: 8px 40px 8px 8px;
}
.capbox-inner {
font: bold 11px arial, sans-serif;
color: #000000;
background-color: #DBF3BA;
margin: 5px auto 0px auto;
padding: 3px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
}
#CaptchaDiv {
font: bold 17px verdana, arial, sans-serif;
font-style: italic;
color: #000000;
background-color: #FFFFFF;
padding: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
}
#CaptchaInput { margin: 1px 0px 1px 0px; width: 135px; }
</style>
function validateform(){
var cm_name=document.supportus_form.cm_name.value;
var cm_email=document.supportus_form.cm_email.value;
var atpos = cm_email.indexOf("@");
var dotpos = cm_email.lastIndexOf(".");
var captha = document.getElementById("txtCaptcha").value;
var capthaValue = document.getElementById('CaptchaInput').value.replace(/ /g,'');
console.log(typeof captha);
if(capthaValue == "" || captha != capthaValue) {
validCaptcha()
}
if (cm_name==null || cm_name=="")
{
alert("Name can't be blank");
return false;
}
else if(atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;
}
function validCaptcha() {
var why = "";
if(capthaValue == ""){
why += "- Please Enter CAPTCHA Code.\n";
}
if(capthaValue != ""){
if((captha != capthaValue) == false){
why += "- The CAPTCHA Code Does Not Match.\n";
}
}
if(why != ""){
alert(why);
return false;
}
}
}
var a = Math.ceil(Math.random() * 9)+ '';
var b = Math.ceil(Math.random() * 9)+ '';
var c = Math.ceil(Math.random() * 9)+ '';
var d = Math.ceil(Math.random() * 9)+ '';
var e = Math.ceil(Math.random() * 9)+ '';
var code = a + b + c + d + e;
document.getElementById("txtCaptcha").value = code;
document.getElementById("CaptchaDiv").innerHTML = code;