1

There is two choices; english or nonenglish. When you don't choose any of them or chose both of them, it is supposed to give error but it doesn't give. Could you please help?Thank you. ps: Even though I make it one checkbox for terms and conditions, alert doesn't work.

JavaScript

function onFormSubmit(form_element) {

        if (form_element.click.checked == false)
                {
                alert("Please check language");
                return false;
                }
    return true;
}

HTML

<form onsubmit="return onFormSubmit(this)">
        Language: <input type="checkbox" name="click" value="english">english
</form>
5
  • 4
    Have you considered using a radio input instead? That way only one can be chosen, and the browser should default to checking one by default. Commented Apr 15, 2012 at 14:05
  • Yes I know it is easier but it is supposed to be checkbox. :( Commented Apr 15, 2012 at 14:08
  • 2
    "Supposed to be" why? For this use-case it really should be radio input. Commented Apr 15, 2012 at 14:41
  • Try not to use click as its a keyword on JS. Commented May 6, 2013 at 7:59
  • In this scenario, radio button should be the choice, not a checkbox. The reason why radio buttons and checkboxes are separated is because of cases like this, where a default option has to be selected, and only one option can be selected at once. You can of course, style your radio button in anyway you desire (even mimic the look of a checkbox, but that would be counterintuitive, no?) Commented Oct 9, 2013 at 18:10

4 Answers 4

1

Condition should be...

if ((form_element.click_1.checked == true && form_element.click_2.checked == true) || (form_element.click_1.checked == false && form_element.click_2.checked == false))

Edit

This is what I used.

<script>
function onFormSubmit(form_element) {

        if ((form_element.click_1.checked == true && form_element.click_2.checked == true) || (form_element.click_1.checked == false && form_element.click_2.checked == false))
                {
                alert("Please check language");
                return false;
                }
    return true;
}
</script>


<form onsubmit="return onFormSubmit(this)">
        Language: <input type="checkbox" name="click_1" value="english">english
                  <input type="checkbox" name="click_2" value="non english">non english</p>
<input type="submit">
</form>
Sign up to request clarification or add additional context in comments.

3 Comments

It didn't solve the problem. When I choose both or none of them, it still doesn't give any error.
When I used this condition, it did show me the alert message for both these conditions...
it doesnt show me any alert. How can I write it any other way?
0
<form onsubmit="return onFormSubmit(this)">
        Language: <input type="checkbox" name="click_1" value="english">english
                  <input type="checkbox" name="click_2" value="non english">non english</p>
        <input type="submit" />
</form>
<script type="text/javascript">
function onFormSubmit(form_element) {
  if ((form_element.click_1.checked == true && form_element.click_2.checked == true)) {
      alert("Please check only one choice!");
      return false;
  } else if ((form_element.click_1.checked == false && form_element.click_2.checked == false)) {
      alert("Please check your choice!");
      return false;
    }
    return true;
}
</script>
​

http://jsfiddle.net/ocanal/3AWUp/

Comments

0

I added a submit button to your HTML and ids on each element

HTML

<form id="myForm">
        Language: <input id="english" type="checkbox" name="click_1" value="english">english
                  <input id="nonEnglish" type="checkbox" name="click_2" value="non english">non english</p>
<button type="submit">Submit</button>
</form>

I attach a function to the onsubmit event the performs validation. Javascript

document.getElementById("myForm").onsubmit =
function () {
        if ((document.getElementById("english").checked == false && document.getElementById("nonEnglish").checked == false) || (document.getElementById("english").checked == true && document.getElementById("nonEnglish").checked == true))
                {
                alert("Please check language");
                return false;
                }
    return true;
}

Demo: http://jsfiddle.net/j3J4E/

1 Comment

Thank you but I have to use the original function.
0

The problem is "onFormSubmit(this)" -- 'this' will not refer to the form when the event is called. If you look up the form from the document instead it will work fine.

<!doctype html>
<html>
<head>
<script type="text/javascript">
function validateForm() {
        var form = document.getElementById("myForm");
        if (!form.language.checked) {
                alert("Please check language");
                return false;
        }
        return true;
}
</script>
</head>
<body>
<form id="myForm" onsubmit="return validateForm()">
        Language: <input type="checkbox" name="language">English</input><br />
        <input type="submit" />
</form>
</body>
</html>

4 Comments

Are you sure you want check boxes? Radio buttons would be better as only one can be selected at a time.
I changed it as terms of conditions and I need checkbox
The reason your function doesn't work is because you're calling it wrong. If you can't change the function change the form: <form id="myForm" onsubmit="return onFormSubmit(document.getElementById('myForm'))">...
Edited to show a working example. Tested in chrome and it works.

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.