2017-09-13 48 views
0

我想使用蜜罐而不是recaptcha來處理垃圾郵件。我正在運行WordPress。通常情況下,我會爲此使用Gravity形式,但是,我沒有開發此網站,他們使用碳字段來填充表單字段。形式:蜜罐垃圾郵件問題

我需要一個條件在下面的JS,即如果$ robotest!=「」回聲「這是垃圾郵件」其他提交表單。我的蜜罐表單字段是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._%+-][email protected][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

除了你所要求的...你的'email_regex'太嚴格了,會排除許多_many_有效的電子郵件地址,包括像'.museum'這樣的較新(較長)頂級域名(TLD)和許多合法字符'[a-z0-9 ._%+ - ] +'在** @ **的左邊 - 請參閱[如何查找或驗證電子郵件地址](http://www.regular-expressions.info /email.html) –

+0

感謝您指出。蜜罐問題的任何想法? – phillystyle123

+0

所以如果我清楚了,'robotest'應該一直是空的,除非機器人發現它並將其填滿。然後,對非空進行測試意味着is_valid應該是false,並且可以在驗證完成後進行測試...... if($ robotest.val().length> 0){is_valid = false; }' –

回答

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; 
} 

如果您的主叫/處理程序設置爲要做好準備。

+0

這兩個選項都不起作用。這裏是我使用的蜜罐字段js: – phillystyle123

+0

'var $ robotest = $ this.find(['[name =「robotest」]']);如果($ robotest.val()。length> 0){ \t $ robotest.attr('style','background-color:rgba(255,40,40,0.25)!important;'); \t is_valid = false; }' – phillystyle123

+0

functions.js正在提交表單。完美工作,直到我添加上面的蜜罐代碼。這裏是更相關的代碼: – phillystyle123