0

I have made a HTML form with several input fields like these:

            <tr>
                <td>inputfield1</td>
                <td><input type="number" class="bestelformulier" name="inputfield1" onkeyup="validateForm()" min="1" max="999"></td>
            </tr>
            <tr>
                <td>inputfield2</td>
                <td><input type="number" class="bestelformulier" name="inputfield2" min="1" max="999"></td>
            </tr>
            <tr>
                <td>inputfield3</td>
                <td><input type="number" class="bestelformulier" name="inputfield3" min="1" max="999"></td>
            </tr>
            <tr>
                <td>inputfield4</td>
                <td><input type="number" class="bestelformulier" name="inputfield4" min="1" max="999"></td>
            </tr>

As you can see my input field 1 has an onkeyup event called 'validateForm()' which does the following:

function validateForm(){
        if(document.forms['form'].inputfield1.value == "")
        {
            document.forms[0].submit.disabled=true;
        }
        else {  
            document.forms[0].submit.disabled=false;
        }
    }

It disables the button if nothing is filled in inputfield1 but enables it if there is something in there.

This works just as it should.

But as you can see i have several inputfields which all need a function like that.

The button needs to be disabled when 0 out of 11 fields are filled but needs to be enabled if even 1 of them is filled (doesn't matter which one)

How do I make it so that if 1 of those 11 fields are filled the button will enable?

All help is grealy appreciated!

2
  • You will have to either store the state of all the other inputs and check each one every time you call validateForm. Otherwise, your best bet is to move the validateForm call to the form "onsubmit" event, and check all the fields then. Commented Jan 21, 2014 at 13:11
  • And what should i place in the validateform itself? since it will still only check input field1 Commented Jan 21, 2014 at 13:13

4 Answers 4

1

Try something like this and add it to the keyup off all the input fields

function validateForm()
{

    var allEmpty = (document.forms['form'].inputfield1.value == "" &&
                    document.forms['form'].inputfield2.value == "" &&
                    document.forms['form'].inputfield3.value == "" &&
                    document.forms['form'].inputfield4.value == "");

    if(allEmpty) // if all are empty disable the button, else enable it.
    {
        document.forms[0].submit.disabled=true;
    }
    else {  
        document.forms[0].submit.disabled=false;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Only problem with this is scaling, if he suddenly needs to add 10 more fields, or all the fields needed a name change, it could become annoying to maintain. Good solution overall :)
You're able to circumvent the min/max this way. You can also proceed with one of the inputs actually being empty
Actually sorry I misread the question, that is allowed. However you can still circumvent the min/max check
@danrichardson what do you mean exactly?
checking for value == "" means that a value of "bob" would be valid, which it isn't, as the value (based on your code) needs to be a number between 1 and 999.
0
var input = document.getElementsByTagName('bestelformulier'); //you can use another class if you want.. as 'required'
var cansubmit = false;
for (i=0; i<input.length; i++)
    if(input[i].value.trim() != "")
        cansubmit = true;

document.forms[0].submit.disabled = cansubmit;

Hope it helps.

1 Comment

sorry about the brace in the end of "for" ... you can remove that.. I will edit it.
0

For achieving this you need to pass the Id of the element to the function.

Make the function call like this

  onkeyup="validateForm('inputfield1')";

apply this for all input fields and update function like below

Try this

function validateForm(eleId){
    var value = document.getElementById(eleId).value;
    if(value == "")
    {
        document.forms[0].submit.disabled=true;
    }
    else {  
        document.forms[0].submit.disabled=false;
    }
}

2 Comments

I'd doubt that would work as the eleId passed would be a string and not an element under document.forms[]
I can't really tell if this one works aswell or not, but i used thetop answer and that one works for sure, thanks for the effort though!
0

Your javascript needs to check the validity of the value also (between 1 and 999). This will also check the form on submit, not just rely on the fields being edited. Full code.

<form name="form" onsubmit="return validateForm()">
    <tr>
            <td>inputfield1</td>
            <td><input type="text" class="bestelformulier" name="inputfield1" onkeyup="validateForm()" min="1" max="999"></td>
        </tr>
        <tr>
            <td>inputfield2</td>
            <td><input type="number" class="bestelformulier" name="inputfield2" onkeyup="validateForm()" min="1" max="999"></td>
        </tr>
        <tr>
            <td>inputfield3</td>
            <td><input type="number" class="bestelformulier" name="inputfield3" onkeyup="validateForm()" min="1" max="999"></td>
        </tr>
        <tr>
            <td>inputfield4</td>
            <td><input type="number" class="bestelformulier" name="inputfield4" onkeyup="validateForm()" min="1" max="999"></td>
        </tr>
        <input type="submit" disabled name="submit" />
</form>

<script type="text/javascript">
    function validateForm() {    
        var isValid = false;
        if (document.forms[0].inputfield1.value > 0 && document.forms[0].inputfield1.value < 1000) {
            isValid = true;
        }
        if (document.forms[0].inputfield2.value > 0 && document.forms[0].inputfield2.value < 1000) {
            isValid = true;
        }
        if (document.forms[0].inputfield3.value > 0 && document.forms[0].inputfield3.value < 1000) {
            isValid = true;
        }
        if (document.forms[0].inputfield4.value > 0 && document.forms[0].inputfield4.value < 1000) {
            isValid = true;
        }
        if (!isValid) {
            document.forms[0].submit.disabled = true;
        } else {
            document.forms[0].submit.disabled = false;
        }
        return isValid;
    }
</script>

Comments

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.