checkbox prob

Results 1 to 3 of 3

Thread: checkbox prob

  1. #1
    zia Guest

    Default checkbox prob

    I am displaying checkboxes on the fly ie they are<BR>being generated dynamically depending on the user<BR>logged in.Some users will have 4 checkboxes & some <BR>may have 8 displayed on their page & so on.<BR>Now this is working good.<BR>But the problem is that it is compulsory for every<BR>user to atleast check 1 checkbox or else a javascript<BR>error should pop up like an alert box when user submits the<BR>form without checking any of the checkboxes.<BR>How do I do this especially when the checkbox is<BR>coming dynamically.<BR>I have used the following to display the checkboxes<BR>dynamically by giving the line below in a for loop<BR>&#060;input type=checkbox name=type&#060;%=i%&#062;&#062;<BR>where the variable &#039;i&#039; will keep incrementing from 1 till number of checkboxes to be shown.As I said it is working but the<BR>only problem is that I am not able to display the error<BR>when no checkbox is checked.<BR>Any Idea.

  2. #2
    Join Date
    Dec 1969

    Default RE: checkbox prob

    &#060;FORM ... onSubmit="return validate(this);"&#062;<BR>..<BR>&#060;INPUT Type=Checkbox Name="type&#060;%=i%&#062;"&#062;<BR>...<BR>&#060; INPUT Type=Checkbox Name="type&#060;%=i%&#062;"&#062;<BR>...<BR>&#060; INPUT Type=Checkbox Name="type&#060;%=i%&#062;"&#062;<BR>&#060;/FORM&#062;<BR><BR>...<BR><BR>&#060;SCRIPT Language=JavaScript&#062;<BR>function validate( frm )<BR>{<BR>&nbsp; &nbsp; var checkCount = 0<BR>&nbsp; &nbsp; // look at all fields in the form<BR>&nbsp; &nbsp; for( var i = 0; i &#060; frm.elements.length; ++i )<BR>&nbsp; &nbsp; {<BR>&nbsp; &nbsp; &nbsp; &nbsp; // our checkboxes have names "typeNN"<BR>&nbsp; &nbsp; &nbsp; &nbsp; if ( frm.elements[i].name.substring(0,4) == "type" )<BR>&nbsp; &nbsp; &nbsp; &nbsp; {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // this is one of our checkboxes<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if it is checked, count it!<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( frm.elements[i].checked ) ++ checkCount;<BR>&nbsp; &nbsp; &nbsp; &nbsp; }<BR>&nbsp; &nbsp; } // end of for loop<BR>&nbsp; &nbsp; // if at least one checked, all is okay<BR>&nbsp; &nbsp; if ( checkCount &#062; 0 ) return true;<BR>&nbsp; &nbsp; // else fail<BR>&nbsp; &nbsp; return false;<BR>}<BR>&#060;/SCRIPT&#062;<BR>

  3. #3
    Join Date
    Dec 1969

    Default also...

    ...if you want to display an error message, then replace the line that says "return false;" with:<BR><BR>alert("You must check at least on of the checkboxes.");<BR>return false;<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts