admin管理员组文章数量:1394096
I need to validate a form using jQuery then submit it to a JavaScript file that redirects it to the confirmation/send mail page. The reason I am doing this is to prevent spam emails with out putting some type of Captcha in. I am basically putting in a wrong url for the post to page of the form and then changing that to the correct page in an external JavaScript file. Everything works great except the form submits before it validates. I can see it mark the errors for a slight second before it submits.
Validation 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>
External JS Redirect:
function submitCommentForm() {
// Change the form action to the real submission page
document.getElementById('contactform').action = "confirmation.php";
// Submit the form
document.getElementById('contactform').submit();
}
I need to validate a form using jQuery then submit it to a JavaScript file that redirects it to the confirmation/send mail page. The reason I am doing this is to prevent spam emails with out putting some type of Captcha in. I am basically putting in a wrong url for the post to page of the form and then changing that to the correct page in an external JavaScript file. Everything works great except the form submits before it validates. I can see it mark the errors for a slight second before it submits.
Validation 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>
External JS Redirect:
function submitCommentForm() {
// Change the form action to the real submission page
document.getElementById('contactform').action = "confirmation.php";
// Submit the form
document.getElementById('contactform').submit();
}
Share
Improve this question
edited Aug 10, 2012 at 14:49
wirey00
33.7k7 gold badges55 silver badges65 bronze badges
asked Aug 10, 2012 at 14:47
pixelJockeypixelJockey
3211 gold badge5 silver badges18 bronze badges
3
- 2 There are better ways to stop spammers from making submissions than what you are doing. Are you ok with the fact your approach (if it was working) would also make it so that users with Javascript disabled would be unable to submit the form? – Paolo Bergantino Commented Aug 10, 2012 at 14:50
- yes I am aware that none js users would not be able to submit the form. What are the better ways? I have been using the jquery validation for the past couple years and received no spam until a few days ago. – pixelJockey Commented Aug 10, 2012 at 14:52
- Do a search for "honeypot" within this site and you will find a couple of questions that might help you. I have implemented them many times in forms with spam problems and have had good success with them. – Paolo Bergantino Commented Aug 10, 2012 at 14:56
2 Answers
Reset to default 5Change
$('#contactform').submit(function() {
return $('#contactform').valid();
});
To
$('#contactform').submit(function() {
if($(this).valid()){
submitCommentForm();
}else{
alert('Please fix the errors within the form.');
}
return false;
});
You could use a jQuery plugin to validate form data.
This is a good one -> Ketchup
版权声明:本文标题:php - Validate form before submit with jQuery post to javaScript then to confirmation page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744595026a2614722.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论