INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
Are you a Computer / IT professional? Join Tek-Tips now!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site

(Download This Button Today!)
Member Feedback
"...You have made an incredible site which is truly a great help to me in solving problems. A tip of my hat to you!..."
Geography
Where in the world do Tek-Tips members come from?
|
Validate ASP Checkbox using Java
|
|
|
Isadore (TechnicalUser) |
11 Apr 03 12:00 |
I need to validate whether a checkbox has been checked after clicking a "submit button" and priot to redirecting the page. What I'd like is a Msgbox to pop up if the checkbox is not checked, and alert the user that it needs to be checked, otherwise, redirect. Here's what i have:
'add attribute in codebehind to button... Me.btnSubmit.Attributes.Add("OnClick","javascript:ValCert()")
'javascript function... function ValCert() if (<<--location of checkbox -->>.check = False){ alert("You must check the certification box to continue"); }
...fairly straight forward. OnClick of button fires java routine; need to "identify" checkbox and then pop up alert message. Thanks for any help on this.
|
|
link9 (Programmer) |
11 Apr 03 13:02 |
Is it perhaps the only checkbox on the page? If so, then it's really simple. Otherwise, it gets a bit more complicated (but not much): //if it's the only one var ind, i, theForm; theForm = document.forms[0]; for(i = 0; i < theForm.length; i++){ if(theForm.elements[i].type == 'checkbox'){ ind = i; break; } } if (!theForm.elements[ind].checked){ alert('You must check the certification box to continue'); } //if it's the second one var ind, i, theForm, numFound; numFound = 0; theForm = document.forms[0]; for(i = 0; i < theForm.length; i++){ if(theForm.elements[i].type == 'checkbox'){ numFound++; if(numFound == 2){ //change to be the nth ind = i; break; } } } if (!theForm.elements[ind].checked){ alert('You must check the certification box to continue'); }  paul   The answer to getting answered -- FAQ855-2992
|
|
|
Isadore (TechnicalUser) |
11 Apr 03 17:40 |
|
|
Isadore (TechnicalUser) |
12 Apr 03 9:38 |
Paul, hate to bother you, but is this particular 'checkbox' an aps or html checkbox? can't seem to get it to fire. Also, do I need to add a "function" line at the beginning, I noticed you have 3 } and only 2 {. I appreciate your time - thanks again. |
|
link9 (Programmer) |
12 Apr 03 11:54 |
There are three brackets.  Opening and closing. But yes, this would be the guts of your existing function, valCert(), so: function ValCert(){ var ind, i, theForm; theForm = document.forms[0]; for(i = 0; i < theForm.length; i++){ if(theForm.elements[i].type == 'checkbox'){ ind = i; break; } } if (!theForm.elements[ind].checked){ alert('You must check the certification box to continue'); } } if it still doesn't work (?), what's it doing or not doing?  paul   The answer to getting answered -- FAQ855-2992
|
|
|
Isadore (TechnicalUser) |
12 Apr 03 17:19 |
Paul: out of office; will test and post back. Thanks for your time. |
|
|
Isadore (TechnicalUser) |
12 Apr 03 21:59 |
Ok, tested it. This is what I have in the codebehind for the aspbutton:
'add attribute in codebehind to button... Me.btnSubmit.Attributes.Add("OnClick","javascript:ValCert()")
...during testing I changed it to this: Me.btnSubmit.Attributes.Add("OnClick","ValCert();")
...and this... Me.btnSubmit.Attributes.Add("OnClick","ValCert(this)")
********
jv code as we have posted (all { and } accn'td for)
********
<asp:Button id="btnSubmit" runat..../>
Button has an OnClick event in the codebehind. The checkbox, the only one on the form, appears as:
<asp:checkbox id="cbxCert" runat="server" Width="751px" Text="Check..."/>
...and other java routines running just find on page.
No alert box however shows up, the page is redirected according to the OnClick event.
I appreciate your time on this Paul; what do you think?
One quickie question while I have your attention; is it necessary in the <asp:Button.../> declaration to put Type="Submit" or is this the default and doesn't need to be hardcoded for a typical OnClick event such as page redirect? |
|
link9 (Programmer) |
12 Apr 03 22:35 |
May I suggest a slightly different approach, then? set the function to run onSubmit for the form, so that the form declaration becomes: <form id=Form1 runat=server onSubmit="return valCert()"> Instead of specifying it for onClick of the button... Change valCert to return a value for you... a slight modification: function ValCert(){ var ind, i, theForm; theForm = document.forms[0]; for(i = 0; i < theForm.length; i++){ if(theForm.elements[i].type == 'checkbox'){ ind = i; break; } } if (!theForm.elements[ind].checked){ alert('You must check the certification box to continue'); return false; } return true; } just the two return statements to short circuit the form if the check fails. You may put a little alert(); line in there first thing just to insure that the javascript is firing, too. Let me know how it works out. -paul   The answer to getting answered -- FAQ855-2992
|
|
|
Isadore (TechnicalUser) |
12 Apr 03 23:52 |
Paul: Other Java routines on page working fine -- I liked this last approach, return T/F on Form submit -- however, no luck, page redirects.
What about putting the java request between the asp tags for the button?
|
|
link9 (Programmer) |
13 Apr 03 8:31 |
No, the javascript call really should be on form submit. If you'll post what your form tag looks like after being rendered (from the html view source), then we can get further. I'm fresh out of ideas on what else might be going wrong w/o seeing that. Or perhaps a link to the page. That would probably be the most helpful. -paul   The answer to getting answered -- FAQ855-2992
|
|
|
Isadore (TechnicalUser) |
14 Apr 03 6:55 |
Thanks Paul. Let me do some more testing, etc... and I'll post back one final time - I'm sure there is enough here to finalize this -- whatever result I'll post back one more time - appreciate your help on this. |
|
|
 |
|