Some required attributes are not validated while using form and java script

I’m using a form for issues with mandatory fields but some aren’t taken into account.

Of the 6 required input blocks only 4 are correctly validated when you try to submit the form. The ‘IVR status’ and the ‘Description’ blocks are not validated.

I’m drawing a blank what I have missed in the code that exclude these blocks from being validated.

function generateEmail(event) {
  event.preventDefault();
  var emailTo = "[email protected]";
  var emailCC = "[email protected]";
  var emailBCC = "[email protected]";
  var emailSubject = "WEBFORM: " + $("#subject").val();
  var emailBody = "Reporter: " + $("#reporter").val() + "%0A%0AThe issue occured on: " + $("#issueDT").val() +
    "%0A%0ACustomer Account Data: " + "%0A   Account: " + $("#accnt_1").val() + "%0A%0ACustomer Account Data: " + "%0A   Account: " + $("#accnt_2").val() + "%0A   Agreement: " + $("#agree_1").val() + "%0A   Agreement: " + $("#agree_2").val() + "%0A   Agreement: " + $("#agree_3").val() + "%0A%0AIVR Status: " + $("#ivrState").val() + "%0A%0ADescription: " + $("#descript").val()
  location.href = "mailto:" + emailTo + "?" +
    (emailCC ? "cc=" + emailCC : "") +
    (emailBCC ? "&bcc=" + emailBCC : "") +
    (emailSubject ? "&subject=" + emailSubject : "") +
    (emailBody ? "&body=" + emailBody : "");
}

function reloadPage() {
  location.reload();
}
.container {
  width: 800px;
  border: 2px solid black;
  padding: 15px;
}

.control {
  color: blue;
}

h1 {
  color: green;
}

h2 {
  color: red;
}
<div class="container">
  <h1>Report your issue</h1>
  </br>
  </br>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <form method="post" class="info" onsubmit="generateEmail(event)">
    Email Subject: </br><input name="subject" placeholder="Subject 20 characters min" id="subject" type="text" size="100" minlength="20" required /></br>
    Reporter Name: </br><input name="reporter" placeholder="Reporter name" id="reporter" type="text" size="100" maxlength="75" required /></br>
    Date & time the issue occured: </br><input name="issueDT" id="issueDT" type="datetime-local" required /></br>
    <label for="ivrState">IVR Status"</label>
    <select name="ivrState" id="ivrStatus" required>
      <option disabled="disabled" selected="selected">Select</option>
      <option value="fail">Failed</option>
      <option value="pass">Passed</option>
    </select>
    </br>
    </br>
    <table>
      <tr>
        <td colspan="5">Customer Account Details:</td>
      </tr>
      <tr>
        <td>Account Nr:</td>
        <td><input type="text" name="accnt_1" placeholder="Account Info" id="accnt_1" required /></td>
        <td> </td>
        <td>Agreement nr:</td>
        <td><input type="text" name="agree_1" placeholder="Agreement Info" id="agree_1" /></td>
      </tr>
      <tr>
        <td>Account Nr:</td>
        <td><input type="text" name="accnt_2" placeholder="Account Info" id="accnt_2" /></td>
        <td> </td>
        <td>Agreement nr:</td>
        <td><input type="text" name="agree_1" placeholder="Agreement Info" id="agree_2" /></td>
      </tr>
      <tr>
        <td> </td>
        <td> </td>
        <td> </td>
        <td>Agreement nr:</td>
        <td><input type="text" name="agree_3" placeholder="Agreement Info" id="agree_3" /></td>
      </tr>
    </table>
    </br>
    Give a description: </br> <textarea rows="7" cols="100" minlength="50" id="descript" name="Description" required>Provide a description of min 50 characters.</textarea></br>
    <input type="reset" value="Reset" onclick="reloadPage()" />     
    <input type="submit" value="Generate email" onclick="generateEmail()" />
  </form>
</div>

3

The select is being marked as valid because the default option has no value attribute. Therefore its value comes from the text contained within the selected option, which is 'Select'. Therefore it’s considered to have a value and is valid.

To fix this, add value="" to the default option:

<select name="ivrState" id="ivrStatus" required>
  <option value="" disabled="disabled" selected="selected">Select</option>
  <option value="fail">Failed</option>
  <option value="pass">Passed</option>
</select>

For the textarea, the minlength attribute is only evaluated when the element has been interacted with by the user (more info on that here). Therefore given the initial state of your page the only validation it has to pass is required, which it does due to your default text. To fix this, move the character limit guidance text to the field’s label.

Your code can also be improved by:

  • Avoiding using <br /> elements and   for spacing. Use CSS to align things properly, as well as using grouping elements such as label. Also it’s <br />, not </br>
  • Use addEventListener() in JS to bind event handlers, not onclick attributes in JS.
  • Bind to the submit event of the form, not the click of the submit button, to handle the email generation.
  • Use string interpolation to build complex strings where you have to. String concatenation gets ugly, quickly.
  • Don’t use table elements for layout. I’ll leave this as an exercise for you to fix
  • If you need to use jQuery, reference it in the <head> of your page, or just before </body>. Do not put it at the start of the <body>

Here’s working example with all those adjustments applied.

const form = document.querySelector('form.info');

form.addEventListener('submit', e => {
  e.preventDefault();
  const emailTo = "[email protected]";
  const emailCC = "[email protected]";
  const emailBCC = "[email protected]";
  const emailSubject = `WEBFORM: ${$("#subject").val()}`;
  const emailBody = `
    Reporter: ${$("#reporter").val()}%0A%0A
    The issue occured on: ${$("#issueDT").val()}%0A%0A
    Customer Account Data: %0A   
    Account: ${$("#accnt_1").val()}%0A%0A
    Customer Account Data: %0A   
    Account: ${$("#accnt_2").val()}%0A   
    Agreement: ${$("#agree_1").val()}%0A
    Agreement: ${$("#agree_2").val()}%0A   
    Agreement: ${$("#agree_3").val()}%0A%0A
    IVR Status: ${$("#ivrState").val()}%0A%0A
    Description: ${$("#descript").val()}`
  location.href = "mailto:" + emailTo + "?" +
    (emailCC ? "cc=" + emailCC : "") +
    (emailBCC ? "&bcc=" + emailBCC : "") +
    (emailSubject ? "&subject=" + emailSubject : "") +
    (emailBody ? "&body=" + emailBody : "");
});

const resetButton = document.querySelector('#resetButton');
resetButton.addEventListener('click', () => {
  window.location.reload();
});
h1 {
  color: green;
  margin-bottom: 40px;
}

h2 {
  color: red;
}

label {
  display: block;
  margin-bottom: 10px;
}

label span {
  margin-bottom: 2px;
}

label span,
label input {
  display: block;
}

table {
  margin-bottom: 20px;
}

.container {
  width: 800px;
  border: 2px solid black;
  padding: 15px;
}

.control {
  color: blue;
}
<div class="container">
  <h1>Report your issue</h1>
  <form method="post" class="info">
    <label>
      <span>Email Subject:</span>
      <input name="subject" placeholder="Subject 20 characters min" id="subject" type="text" size="100" minlength="20" required />
    </label>
    <label>
      <span>Reporter Name:</span>
      <input name="reporter" placeholder="Reporter name" id="reporter" type="text" size="100" maxlength="75" required />
    </label>
    <label>
      <span>Date & time the issue occured:</span>
      <input name="issueDT" id="issueDT" type="datetime-local" required />
    </label>
    <label>
      <span>IVR Status</span>
      <select name="ivrState" id="ivrStatus" required>
        <option disabled="disabled" selected="selected" value="">Select</option>
        <option value="fail">Failed</option>
        <option value="pass">Passed</option>
      </select>    
    </label>

    <table>
      <tr>
        <td colspan="5">Customer Account Details:</td>
      </tr>
      <tr>
        <td>Account Nr:</td>
        <td><input type="text" name="accnt_1" placeholder="Account Info" id="accnt_1" required /></td>
        <td> </td>
        <td>Agreement nr:</td>
        <td><input type="text" name="agree_1" placeholder="Agreement Info" id="agree_1" /></td>
      </tr>
      <tr>
        <td>Account Nr:</td>
        <td><input type="text" name="accnt_2" placeholder="Account Info" id="accnt_2" /></td>
        <td> </td>
        <td>Agreement nr:</td>
        <td><input type="text" name="agree_1" placeholder="Agreement Info" id="agree_2" /></td>
      </tr>
      <tr>
        <td> </td>
        <td> </td>
        <td> </td>
        <td>Agreement nr:</td>
        <td><input type="text" name="agree_3" placeholder="Agreement Info" id="agree_3" /></td>
      </tr>
    </table>

    <label>
      <span>Give a description (min 50 chars): </span>
      <textarea rows="7" cols="100" minlength="50" id="descript" name="Description" required></textarea>
    </label>

    <button type="button" id="resetButton">Reset</button>
    <button type="submit">Generate email</button>
  </form>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật