2012-08-10 41 views
0

我需要使用jQuery验证表单,然后将其提交给JavaScript文件,该文件将其重定向到确认/发送邮件页面。我这样做的原因是为了防止垃圾邮件无法将某些类型的验证码放入。我基本上是为表单的页面发送错误的URL,然后将其更改为外部JavaScript文件中的正确页面。除非表单在验证之前提交,否则一切都很好。我可以看到它在提交之前稍微标记错误。使用jQuery提交到javaScript之前验证表单然后到确认页面

验证JS:

<script type="text/javascript"> 
$(document).ready(function(){ 

    $('#contactform').submit(function() { 
     return $('#contactform').valid(); 
    }); 

     $("#contactform").validate(); 

      $("#Name").rules("add", { 
     required: true, 
     minlength: 5, 
     messages: { 
     required: "Required", 
     minlength: jQuery.format("Minimum {0} Characters Required") 
    } 
    }); 
     $("#Question").rules("add", { 
     required: true, 
     minlength: 20, 
     messages: { 
     required: "Required", 
     minlength: jQuery.format("Question Must Be {0} Character Long") 
    } 

    }); 

    $("#math").rules("add", { 
     required: true, 
     equalTo: "#math2", 
     messages: { 
     required: "Solve Security Question.", 
    } 
    }); 


}); 
</script> 

外部JS重定向:

function submitCommentForm() { 
    // Change the form action to the real submission page 
    document.getElementById('contactform').action = "confirmation.php"; 
    // Submit the form 
    document.getElementById('contactform').submit(); 
} 
+2

有更好的方法来作出比提交你在做什么阻止垃圾邮件。你确定你的方法(如果它的工作原理),你也可以这样做,那么禁用JavaScript的用户将无法提交表单? – 2012-08-10 14:50:01

+0

是的,我知道没有js用户将无法提交表单。有什么更好的方法?过去几年我一直在使用jquery验证,并在几天前才收到垃圾邮件。 – pixelJockey 2012-08-10 14:52:55

+0

在本网站中搜索“蜜罐”,你会发现一些可能对你有帮助的问题。我曾多次以垃圾邮件的形式实施这些方案,并取得了很好的成功。 – 2012-08-10 14:56:33

回答

5

变化

$('#contactform').submit(function() { 
    return $('#contactform').valid(); 
}); 

$('#contactform').submit(function() { 
    if($(this).valid()){ 
    submitCommentForm(); 
    }else{ 
    alert('Please fix the errors within the form.'); 
    } 
    return false; 
}); 
0

你可以使用jQuery插件来验证表单数据。

这是一个很好的 - >Ketchup

相关问题