0

I want to use js to check a form in the case that none of my checkboxes are checked and the input field hasn't been filled. When this is the case, the user should get a message that tells them to make at least one choice or formulate their own.

The form elements...

<input type="checkbox" name="checkboxA" id="checkboxA" value="X" />
<input type="checkbox" name="checkboxB" id="checkboxB" value="Y" />
<input type="checkbox" name="checkboxC" id="checkboxC" value="Z" />

<input type="text" id="Text" class="textfield" value="please enter text." maxlength="100" size="40" onblur="if (this.value == '') {this.value = 'please enter text';}"  onfocus="if (this.value == 'please enter text') {this.value = '';}" />

...are checked by this js function when the POST method is performed:

function checkFormElement() {
if ((document.getElementById("text").value == "please enter text.")
    && (document.getElementById("checkboxA").checked == false)
    && (document.getElementById("checkboxB").checked == false)
    && (document.getElementById("checkboxC").checked == false)){
        alert("Please choose out of X Y Z or formulate your own choice .");
        document.getElementById("checkboxA").focus();
    return false;
    }
return true;

}

I guess using jQuery should be much easier but I still love to know how to solve this in pure js.

1
  • 3
    So, what is your question? What are you having problems with in your code? What doesn't work? Commented Aug 21, 2012 at 20:09

3 Answers 3

3

Case matters with ID values so change this:

document.getElementById("text").value

to this to match what you have in your HTML:

document.getElementById("Text").value

Once I fix the case issue and put in code to actually call checkFormElement(), it seems to work fine here: http://jsfiddle.net/jfriend00/9DHxF/

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

4 Comments

or change the html to "text" :)
Problem is that it doesn't give me the alert message. Checked for typos (such as the given uppercase lowercase) but still no succes..
@Kiwi1 - Are you calling checkFormElement() somewhere? It isn't called in the code you have in your question. When I call it and fix the case error, it works here: jsfiddle.net/jfriend00/9DHxF.
@jfriend00: looked again at my code and found out I still had some typos here and there. should be looking out for these before hitting the forums :). Thanks anyway!
0

You're missing a submit or similar type of button\option that will run checkFormElement(). Once you include something that calls this function, you shouldn't experience any problems.

Comments

0

You have a few errors in your code. Mostly mismatched IDs and such.

Working example here: http://jsfiddle.net/E8SBq/

This should work for you:

<input type="checkbox" name="checkboxA" id="checkboxA" value="X" />
<input type="checkbox" name="checkboxB" id="checkboxB" value="Y" />
<input type="checkbox" name="checkboxC" id="checkboxC" value="Z" />

<input type="text" id="Text" class="textfield" value="please enter text" maxlength="100" size="40" onblur="if (this.value == '') {this.value = 'please enter text';}"  onfocus="if (this.value == 'please enter text') {this.value = '';}" />
<input type="submit" value="Submit" onClick="checkFormElement()" />

And the JS function:

function checkFormElement() {
if (document.getElementById("Text").value != null  
    && document.getElementById("checkboxA").checked == false
    && document.getElementById("checkboxB").checked == false
    && document.getElementById("checkboxC").checked == false) {
        alert("Please choose out of X Y Z or formulate your own choice .");
        document.getElementById("checkboxA").focus();
    return false;
    }
return true;

}

1 Comment

Please note, the onblur event of the text field prevents a null value. So you should keep Kiwi1's original code. Also in my experience, the submit button works better in a <form>. You should be able to get the same functionality with a regular button and not trigger any possible form actions.

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.