在函数中使用IF语句

时间:2015-09-14 14:13:36

标签: javascript forms validation

我正在尝试对表单进行一些JS验证,以确保电子邮件匹配。 我可以让这个用于填写的第一组电子邮件地址,但可以选择添加其他用户(表单在售票站点上)。但是,代码似乎没有检查脚本中的第二个或第三个电子邮件地址字段。

这是我创建的功能:

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: 							</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							</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>

1 个答案:

答案 0 :(得分:0)

似乎你想要的逻辑是&#34; OR&#34;

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