0

Edit: this is not a duplicate, because I am using HTML5 validating and the required attribute, and when the input is hidden with the modal, the required attribute is removed from it. I only have it as required when the fixed-term radio button is clicked and during the time the modal is open. When submitting the form, it is not required anymore (although this might not be the right way to solve this).

/*Change expiration date input to required once the fixed term choice is 
taken, change back when open-ended is chosen*/

function updateRequired() {
document.getElementById('expdate').required = true;
}

function updateNonRequiredCancel() {
document.getElementById('expdate').removeAttribute('required');
radiobtn = document.getElementById("openend");
radiobtn.checked = true;
}

function updateNonRequired() {
document.getElementById('expdate').removeAttribute('required');
}

My problem:

Seen some posts on this topic, but no solutions have worked yet. I have a form with many inputs I'm trying to validate. Currently I am stuck on validating an input inside a bootstrap modal, since it keeps giving me the error: "An invalid form control with name='expdate' is not focusable." when I try to submit the form. All other inputs validate fine outside the radio buttons and modal.

The modal is toggled when the radio button "fixed-term" is chosen and contains the input to choose an expiration date. I have some onclick functions in there for: 1) when open-ended is clicked, 'expdate' is no longer required 2) when fixed-date is chosen, 'expdate' is required 3) when cancel is clicked inside the modal, 'expdate' is no longer required and the radio button selection goes to open-ended automatically 4) when save changes is clicked I validate whether the 'expdate' field is empty or not.

These onclick functions work fine, for example I can't save changes inside the modal without entering an expiration date. Only problem arises when trying to submit the whole form.

<fieldset>
    <div class="form-check form-check-inline">
         <input type="radio" required class="form-check-input" id="openend" 
name="term" onClick="updateNonRequired();">
         <label class="form-check-label" for="openend">
            Open-ended
         </label>
    </div>
    <div class="form-check form-check-inline">
         <input type="radio" class="form-check-input" 
onclick="updateRequired();" id="fixed-term" name="term" data-toggle="modal" 
data-target="#dateModal">
         <label class="form-check-label" for="fixed-term">
                Fixed-term
         </label>

         <div class="modal fade" id="dateModal" tabindex="-1" role="dialog" aria-hidden="true">
         <div class="modal-dialog modal-dialog-centered" role="document">
               <div class="modal-content">
                     <div class="modal-header">
                          <h4 class="end-date" id="end-date">Pick the end-date</h4>
               </div>
               <div class="modal-body">
                      <label for="expdate">
                              Enter a date of expiration:
                      </label>
                      <input type="date" oninvalid="InvalidTerm(this);" oninput="InvalidTerm(this);" id="expdate" class="form-control" name="expdate" min="2000-01-02" max="2020-01-01">
                       <br>
                       </div>
                       <div class="modal-footer">
                           <button type="button" class="btn btn-secondary" data-dismiss="modal" onclick="updateNonRequiredCancel();">Close</button>
                           <button type="button" class="btn btn-primary" onclick="ValidateModal();">Save changes</button>
                       </div>
                   </div>
              </div>
         </div>
    </div>
</fieldset>

And some Javascript I'm using to try and validate 'expdate' while the modal is open which is not working, except for checking if the field is empty (which sets var isFine to true and let's the save button hide the modal):

var isFine = false;
function InvalidTerm(datebox) {
var date = new Date(datebox.value);

if (!isNaN(date.getTime())) {
    datebox.setCustomValidity('Required field!');
}
else if (datebox.validity.rangeOverflow || datebox.validity.rangeUnderflow){
    numberbox.setCustomValidity('Date must be between today and 2020-01- 
01');
}
else {
    datebox.setCustomValidity('');
}
isFine=true;
return true;
}       

function ValidateModal(){
     if (isFine === true) {
          //document.getElementById('expdate').removeAttribute('required'); //questionable
          $('#dateModal').modal('hide');
     }
     else{
          alert('no no');
     }
 }

Any help is appreciated!

Tuts
  • 79
  • 1
  • 10
  • Possible duplicate of [An invalid form control with name='' is not focusable](https://stackoverflow.com/questions/22148080/an-invalid-form-control-with-name-is-not-focusable) – MakeLoveNotWar Oct 24 '18 at 19:09
  • 1
    Added an edit explaining why that question (and solution) does not apply here. – Tuts Oct 24 '18 at 19:27
  • i can't see in your code where you are setting/disabling `required` attribute. you should try changing `required` dynamically to match your needs, `input.required = false / true` – MakeLoveNotWar Oct 24 '18 at 19:32
  • I wrote in my first edit that I have four functions for that, but didn't submit those as code. Added those snippets of js as well, apologies for not doing it before! – Tuts Oct 24 '18 at 20:04
  • `document.getElementById('expdate').required = false` try adding this to `updateNonRequiredCancel` after removing attribute – MakeLoveNotWar Oct 24 '18 at 20:06
  • `false` does not apply to a boolean attribute such as required. Removing the attribute the way I do is the only way, as explained here: https://stackoverflow.com/questions/48651166/validation-required-true-false-html-form – Tuts Oct 24 '18 at 20:14

0 Answers0