2015-09-14 115 views
-1

我正在嘗試在表單上執行一些JS驗證以確保電子郵件匹配。 我可以得到這個工作的第一組電子郵件地址填寫,但可以選擇添加額外的用戶(表單是在售票網站上)。但是,該代碼似乎並未檢查腳本中的第二個或第三個電子郵件地址字段。在函數中使用IF語句

這功能我已經創建:

function checkEmail(theForm) { 
    if (theForm.email.value !== theForm.TEXT_12.value) { 
    alert('Those emails don\'t match!'); 
    return false; 
    } else if (theForm.x_attendee_email[2].value !== theForm.x_attendee_TEXT_12[2].value) { 
    alert('Those emails don\'t match!'); 
    return false; 
    } else if (theForm.x_attendee_email[3].value !== theForm.x_attendee_TEXT_12[3].value) { 
    alert('Those emails don\'t match!'); 
    return false; 
    } else { 
    return true; 
    } 
} 

而且在表單標籤我已經加入:onsubmit="return checkEmail(this);"

任何人看到和問題與功能我已經創建了?

下面是HTML:

<form onsubmit="return checkEmail(this);" id="registration_form" action="**********************" method="post"> 
 
    <p class="event_prices"> 
 
    <label for="event_cost"><span class="event_price_label section-title">Choose your ticket:</span> 
 
    </label> 
 
    <select id="price_option-1" name="price_option"> 
 
     <option value="17|Ticket (Early Bird 1)">Ticket (Early Bird 1)</option> 
 
     <option value="18|Tickets (Early Bird 2)">Tickets (Early Bird 2)</option> 
 
    </select> 
 
    <input type="hidden" value="true" id="price_select-1" name="price_select"> 
 
    </p> 
 
    <p class="event_time"> 
 
    <span class="span_event_time_label">Start Time:</span> <span class="span_event_time_value">7:45 am</span> 
 
    <br><span class="span_event_time_label">End Time: </span> <span class="span_event_time_value">8:00 pm</span> 
 
    <input type="hidden" value="9" id="start_time_id_9" name="start_time_id"> 
 
    </p> 
 
    <p class="start_date"> 
 
    <span class="span_event_date_label"> 
 
    Date: \t \t \t \t \t \t \t </span> 
 
    <span class="span_event_date_value"> Date****** </span> 
 
    </p> 
 
    <div class="event-reg-form-groups" id="event-reg-form-groups"> 
 
    <h3 class="section-heading"> 
 
     Registration Details \t \t \t \t \t \t \t </h3> 
 
    <fieldset id="personal-information-1442231474" class="event_questions"> 
 
     <h4 class="reg-quest-title section-title">Personal Information</h4> 
 
     <div class="event_form_field"> 
 
     <label class="ee-reg-page-questions" for="fname">First Name<em>*</em> 
 
     </label> 
 
     <input type="text" value="" name="fname" id="fname-1-0-1" class=" required ee-reg-page-questions ee-reg-page-text-input fname" title=""> 
 
     </div> 
 
     <div class="event_form_field"> 
 
     <label class="ee-reg-page-questions" for="lname">Last Name<em>*</em> 
 
     </label> 
 
     <input type="text" value="" name="lname" id="lname-1-0-1" class=" required ee-reg-page-questions ee-reg-page-text-input lname" title=""> 
 
     </div> 
 
     <div class="event_form_field"> 
 
     <label class="ee-reg-page-questions" for="email">Email<em>*</em> 
 
     </label> 
 
     <input type="text" value="" name="email" id="email-1-0-1" class=" required email ee-reg-page-questions ee-reg-page-text-input email" title=""> 
 
     </div> 
 
     <div class="event_form_field"> 
 
     <label class="ee-reg-page-questions" for="TEXT_12">Email Confirmation<em>*</em> 
 
     </label> 
 
     <input type="text" value="" name="TEXT_12" id="TEXT_12-1-0-1" class=" required ee-reg-page-questions ee-reg-page-text-input TEXT_12" title=""> 
 
     </div> 
 
     <div class="event_form_field"> 
 
     <label class="ee-reg-page-questions" for="TEXT_11">Company<em>*</em> 
 
     </label> 
 
     <input type="text" value="" name="TEXT_11" id="TEXT_11-1-0-1" class=" required ee-reg-page-questions ee-reg-page-text-input TEXT_11" title=""> 
 
     </div> 
 
     <div class="event_form_field"> 
 
     <label class="ee-reg-page-questions" for="phone">Phone<em>*</em> 
 
     </label> 
 
     <input type="text" value="" name="phone" id="phone-1-0-1" class=" required ee-reg-page-questions ee-reg-page-text-input phone" title=""> 
 
     </div> 
 
    </fieldset> 
 
    </div> 
 
    <input type="hidden" value="N" name="use_coupon[1]"> 
 
    <input type="hidden" value="N" name="use_groupon[1]"> 
 
    <input type="hidden" value="post_attendee" id="regevent_action-1" name="regevent_action"> 
 
    <input type="hidden" value="1" id="event_id-1" name="event_id"> 
 
    <div class="event_form_field additional_header espresso_add_subtract_attendees" id="additional_header"><a class="add-additional-attendee-lnk additional-attendee-lnk ui-state-highlight" rel="1" id="add-additional-attendee-1">Add More Attendees?</a> 
 
    </div> 
 
    <input type="hidden" value="f77e3810c8" name="reg_form_nonce" id="reg_form_nonce"> 
 
    <input type="hidden" value="/event-registration/?ee=1" name="_wp_http_referer"> 
 
    <p id="event_form_submit-1" class="event_form_submit"> 
 
    <input type="submit" value="Submit" name="Submit" id="event_form_field-1" class="btn_event_form_submit ui-button ui-button-big ui-priority-primary ui-state-default ui-state-hover ui-state-focus ui-corner-all"> 
 
    </p> 
 
</form>

+0

刪除'else's。 – Teemu

+0

您應該附上表格和相關字段的html。是否有多個字段以x_attendee_email作爲名稱?你確定要定位第三和第四個輸入嗎? (元素是零索引在幾乎任何編碼,你會在js中做)。 – scrappedcola

+0

當您在使用它作爲函數的參數之前聲明並分配'theForm'時,還要添加代碼。 –

回答

0

看來你想在這裏的邏輯是 「或」

function checkEmail(theForm) { 
    if (theForm.email.value !== theForm.TEXT_12.value) || 
    (theForm.x_attendee_email[2].value !== theForm.x_attendee_TEXT_12[2].value) || 
    (theForm.x_attendee_email[3].value !== theForm.x_attendee_TEXT_12[3].value) { 
    alert('Those emails don\'t match!'); 
    return false; 
    } else { 
    return true; 
    } 
} 
+0

我試過「或」,但不幸的是它沒有工作。 –

+0

@ RobertO'Halloran我看了你的HTML - 完全不清楚新的參與者如何被添加到這個表單。照顧開導我們? – Hogan