我想使用honeypot代替recaptcha进行垃圾邮件。我正在运行WordPress。通常情况下,我会使用重力形式,但是,我没有开发这个网站,他们使用碳素填充表单字段。
我需要以下js中的条件,即如果$ robotest!=“”echo“这是垃圾邮件”其他提交表单。我的蜜罐表单字段是robotest(var $ robotest = $ this.find(['[name =“robotest”]']);)。
以下是提交表单的整个代码块:
$('.form-suite form').on('submit', function(e) {
e.preventDefault();
var is_valid = true;
var data_to_send = {};
var $this = $(this);
console.log('FORM SUBMITTED!!!!');
console.log('ap test');
console.log($('#event_dates').val());
console.log(grecaptcha);
var $team_artist_event = $this.find('[name="team-artist-event"]');
var $booking_event = $this.find('[name="game"]');
var $first_name = $this.find('[name="fname"]');
var $last_name = $this.find('[name="lname"]');
var $company = $this.find('[name="company"]');
var $email = $this.find('[name="email"]');
var $phone = $this.find('[name="phone"]');
var $contact_method = $this.find('[name="contact-method"]');
var $comments = $this.find('[name="comments"]');
var $sell_suite = $this.find(['[name="sell_suite"]']);
var $corporate_outings = $this.find(['[name="corporate_outings"]']);
var $event_dates = $this.find(['[name="event_dates"]']);
var $how_many = $this.find(['[name="how_many"]']);
var $robotest = $this.find(['[name="robotest"]']);
console.log('AARON HI dates:' + $event_dates.val());
$this.find('.required').each(function() {
$(this).attr('style', 'background-color: #ddd;');
var field_value = $(this).val();
if (field_value == '') {
$(this).attr('style', 'background-color: rgba(255, 40, 40, 0.25) !important;');
is_valid = false;
}
});
//var captcha = grecaptcha.getResponse();
//if (captcha == '') {
//is_valid = false;
//$('.captcha').attr('style', 'background-color: rgba(255, 40, 40, 0.25) //!important;');
//}
var email_regex = /[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;
if (!email_regex.test($email.val())) {
$email.attr('style', 'background-color: rgba(255, 40, 40, 0.25) !important;');
is_valid = false;
}
if (is_valid) {
data_to_send.team_artist_event = $team_artist_event.val();
data_to_send.booking_event = $booking_event.val();
data_to_send.first_name = $first_name.val();
data_to_send.last_name = $last_name.val();
data_to_send.company = $company.val();
data_to_send.email = $email.val();
data_to_send.phone = $phone.val();
data_to_send.contact_method = $contact_method.val();
data_to_send.comments = $comments.val();
data_to_send.sell_suite = $sell_suite.val();
data_to_send.corporate_outings = $corporate_outings.val();
data_to_send.action = 'booking_form';
data_to_send.event_dates = $('#event_dates').val();
data_to_send.how_many = $('#how_many').val();
$.ajax({
url: ajax_object.ajax_url,
data: data_to_send,
method: 'POST',
dataType: 'json',
success: function(data) {
if (data.status == 'OK') {
console.log('GA send');
ga('send', {
'hitType': 'pageview',
'page': '/form-submit/team' // Virtual page (aka, does not actually exist) that you can now track in GA Goals as a destination page.
});
ga('send', 'event', 'Forms', 'submitted', 'Team Form');
window.location.href = '/thank-you.php';
//$('.personal-info, .event-info').hide();
//$(' .event-info').before('<h2 class="form-success-msg">' + data.message + '</h2>');
}
},
});
}
});
答案 0 :(得分:0)
如果人类填写表单,蜜罐字段<input name="robotest">
应该始终为空,因为该字段远离人类。盲目地填写表格的机器人会(可能)找到该字段并在其中加入一些价值。
您的示例已将您的验证码代码注释掉......
//var captcha = grecaptcha.getResponse();
//if (captcha == '') {
//is_valid = false;
代替那些你可以只测试蜜罐字段以确保它是空的。
if ($robotest.val().length > 0) {
is_valid = false;
}
您可以在!is_valid
时继续跳过其余的测试(提前退回),或者甚至立即返回:
if ($robotest.val().length > 0) {
return;
}
如果您的调用者/处理程序已设置为准备就绪。