0
<form action="/cgi-bin/Lib.exe" method=POST name="checks">  

     <input type=checkbox name="user1" value="'$NAME'">      
     <input type=checkbox name="user2" value="'$NAME'">     
     <input type=checkbox name="user3" value="'$NAME'">     

<input type="button" value="User 1" onclick="somefunction()">

For example, if I selected checkbox user2 I would want the javascript function to pop up saying "you are not user 1..." (all input check boxes are under same form name).

After validation of specific check box name I will do document.checks.submit();

Thanks.

3
  • If you already know a particular checkbox isn't valid for the user, then why would you offer it ? Commented Feb 2, 2009 at 22:51
  • My example is modified for the question. I have a set of radio buttons for sorting and a set of check boxes for deleting. I have a sort button and and a delete button. If you select the sort and hit delete it submits the form right now. Both buttons do the same submit. Commented Feb 2, 2009 at 22:58
  • okay .. never mind .. :) Commented Feb 2, 2009 at 23:53

3 Answers 3

2

I'd propose few improvements to sktrdie's reply. This will avoid submitting form on errors.

<form action="".... onsubmit="return somefunction(this);">

function somefunction(f) {
    var user1 = f.user1;
    var user2 = f.user2;
    // etc..
    if(user2.checked) {
        alert("you are not user1");
        return false;
    } 
    return true;
}

Note #1: this example is very simple and not-so-flexible, so additional reading on forms validation would be good idea. Say, on w3schools

Note #2: do not forget to implement server-side validation along with this. JS checks can be easily avoided.

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

Comments

1
<form onsubmit="somefunction(this);" ...

function somefunction(f) {
    var user1 = f.user1;
    var user2 = f.user2;
    // etc..
    if(user2.checked) alert("you are not user1");
}

Comments

0

You may want to use the checkbox's onclick event to do the validation... So when they click it to turn it on/off you can catch em immediately, before form submission.

<input type=checkbox name="user1" value="'$NAME'" onclick="javascript:validatecheckbox(document.checks.user1);">    

And then whatever you want to validate against in the JS function: (sorry for some reason the encoding of this code isn't working right... but hopefully you get the idea)

<script language="javascript">
    function validatecheckbox(inputbox) {
        if (inputbox.checked)
            alert ('Are you ' + inputbox.name + '?');
    }
</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.