1

Basically, I have two input controls a textbox and a checkbox. What I want to happen is when the checkbox is clicked, I want to set the value of the input. The javascript I have sets the default value however when I submit, the form fails validation for the input text to be required even though the text input has the value.

 function NoLienHolder() {
            var lh = document.getElementById('vehicleLienHolder');
            if (lh != null) {
                lh.value = "NONE";
            }
        }
<input name="vehicleLienHolder" id="vehicleLienHolder" required="required" placeholder="Enter Lien Holder" oninvalid="this.setCustomValidity('Please enter the Lien Holder or select no lien holder.')"
        oninput="this.setCustomValidity('')" /><input type="checkbox" title="No Lien Holder" onclick="NoLienHolder()" name="NoLienHolderCheckBox" id="NoLienHolderCheckBox" />No Lien Holder

1 Answer 1

1

This is the code you have,

function NoLienHolder() {
  var lh = document.getElementById('vehicleLienHolder');
  if (lh != null) {
    lh.value = "NONE";
  }
}
<form>
  <input name="vehicleLienHolder" id="vehicleLienHolder" required="required" placeholder="Enter Lien Holder" oninvalid="this.setCustomValidity('Please enter the Lien Holder or select no lien holder.')" oninput="this.setCustomValidity('')" />
  <input type="checkbox" title="No Lien Holder" onclick="NoLienHolder()" name="NoLienHolderCheckBox" id="NoLienHolderCheckBox" />No Lien Holder
  <input type="submit" value="Submit">
</form>
When I ran it, I cannot reproduce the problem (it passes the validation normally), but I could see a few issues. I have modified you code to produce what I think might be a better solution.

var original = "";
function NoLienHolder() {
  var lh = document.getElementById('vehicleLienHolder');
  var checkbox = document.getElementById("NoLienHolderCheckBox");
  if (checkbox.checked) {
    original = lh.value;
    document.getElementById('status').innerHTML = "Uncheck it to restore its original value";
    lh.value = "NONE";
    lh.setAttribute("disabled", "disabled");
    lh.removeAttribute("required");
  } else {
    lh.value = original;
    document.getElementById('status').innerHTML = "Check the Checkbox if there is no holder";
    lh.setAttribute("required", "required");
    lh.removeAttribute("disabled");
  }
}
<form>
  <input name="vehicleLienHolder" id="vehicleLienHolder" required="required" placeholder="Enter Lien Holder" oninvalid="this.setCustomValidity('Please enter the Lien Holder or select no lien holder.')" oninput="this.setCustomValidity('')" /> <input type="checkbox"
    title="No Lien Holder" onclick="NoLienHolder()" name="NoLienHolderCheckBox" id="NoLienHolderCheckBox" />No Lien Holder
  <input type="submit" value="Submit">
  <span id="status">Check the Checkbox if there is no holder</span>
</form>

If you run it, fill in the input field manually, and submit, it will work like normal. If you don't fill in and don't check the box, the required validation will still come into effect. If you don't fill it and check the box, the text will become "NONE", and if the problem reemerges, then it will still submit, and the backend can process that.

In your original answer, you checked if the input field is empty before setting it to NONE, but if the user filled something in, and then checked the check box, what will happen? It would submit, but instead of getting NONE, you would get the user's input instead, although the checkbox is checked. So the best way is probably to save the user value, and restore it upon unchecking.

First of all, you probably want to check if the user clicked on the checkbox to check it or to uncheck it, because your script would set it the text field to "NONE" even if the user checked the checkbox, changed his mind, changed the text, and clicked on it to uncheck. Additionally, lh != null will not work, because lh is the element, not its value. You need to access its value attribute. Additionally, you might want to consider removing the required attribute from the element, setting it to empty, and disabling it when the user checks it. This prevents any issues if the user manually inputs "NONE" in the field but leaves the input box unchecked. After all, your request would send the state of the check box as well.

Sign up to request clarification or add additional context in comments.

8 Comments

And why don’t you use jquery? That will make this much easier. And I have no idea why you have that problem. If it really becomes necessary, I suppose you could delete that element, and create a new one with the text already set, but I am not an expert, so I am not sure if that will work.
Because HTML5 has the validation for pattern and required for a reason. why I don't understand is how come when i set the value via javascript the html5 valdiation fails.
Me neither. I have encountered this myself. But html5 might have these attributes for a reason, but you may not have a reason to use them in certain cases. What you are doing with NONE is practically the same as not requiring the field
Not to get into a huge argument, but i'm tailoring this UI to a specific userbase. If i don't handle it now, they are going to call me and tell me to update those values (and often). I don't want to field these calls.
Temporarily removing the required attribute does not matter as long as you re-add this when the user uncheck the box and disable it while it is not required. This means that the user will be able to edit the field normally as long as they did not check the box, and if they do not check it and fill in nothing, the required attribute will still come into effect and prevent them from submitting. If they did, it would stay NONE.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.