views:

267

answers:

4
<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.

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

function somefunction(f) {
    var user1 = f.user1;
    var user2 = f.user2;
    // etc..
    if(user2.checked) alert("you are not user1");
}
Luca Matteis
+2  A: 

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.

Sergii
+1  A: 

If you already know a particular checkbox isn't valid for the user, then why would you offer it ?

Scott Evernden
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.
Tommy
okay .. never mind .. :)
Scott Evernden
A: 

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 (r u ' + inputbox.name + '?'); }