Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validator for checkbox 1

Status
Not open for further replies.

James1981

Programmer
Nov 2, 2002
76
US
Hi,

I need to make sure that a checkbox is checked before a user can submit a form. is there any built-in validator that can handle a checkbox or will I have to do it manually in my code-behind?

Cheers
James.
 
James,

What's the point of this? Isn't submitting the form equivalent to checking the box and submitting the form? It seems to be a superfluous action.

If not, you could use a validator to check?

Craig
 
The checkbox is to confirm that the user has read the terms and conditions. There is no validator that will handle checkboxes - i've checked. I have decided to use code-behind instead, and check the value of the checkbox in question before submitting the form.

Cheers
James
 
Why not disable the button, and then with Javascript enable the button on the click event of the check box.

Here is some a simple sample to help you get started:

Web page:
---------

<%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;WebForm1.aspx.vb&quot; Inherits=&quot;CheckBoxTest.WebForm1&quot;%>
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content=&quot;Microsoft Visual Studio.NET 7.0&quot; name=&quot;GENERATOR&quot;>
<meta content=&quot;Visual Basic 7.0&quot; name=&quot;CODE_LANGUAGE&quot;>
<meta content=&quot;JavaScript&quot; name=&quot;vs_defaultClientScript&quot;>
<meta content=&quot; name=&quot;vs_targetSchema&quot;>
<script language=&quot;javascript&quot;>
function EnableButton()
{
document.Form1.Button1.disabled=false;
}
</script>
</HEAD>
<body MS_POSITIONING=&quot;GridLayout&quot;>
<form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;>
<asp:button Enabled=False id=&quot;Button1&quot; style=&quot;Z-INDEX: 101; LEFT: 259px; POSITION: absolute; TOP: 154px&quot; runat=&quot;server&quot; Text=&quot;Button&quot;></asp:button>
<asp:CheckBox id=&quot;CheckBox1&quot; style=&quot;Z-INDEX: 102; LEFT: 264px; POSITION: absolute; TOP: 190px&quot; runat=&quot;server&quot; Text=&quot;Enable Button&quot; ></asp:CheckBox></form>
</body>
</HTML>

Code Behind:

Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents CheckBox1 As System.Web.UI.WebControls.CheckBox
Protected WithEvents Button1 As System.Web.UI.WebControls.Button

#Region &quot; Web Form Designer Generated Code &quot;

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Me.CheckBox1.Attributes.Add(&quot;onClick&quot;, &quot;javascript:EnableButton()&quot;)
End Sub

End Class
 
Thanks for the advice... I decided just to do a check within the code behind for the onclick event of the button to make sure that the checkbox was checked.

If the unchecked I would display and error message and not allow the user to continue. This seemed to be the easiest solution.

Thanks again,
James
 
my $.02...

users prefer client side stuff, rather than being told after they've submitted a form that they did something wrong. The disable/enabling the button is an easy, clever way to do it, and is how i handle those situations.

just my opinion.
 
Hi gagz,

I do admit you have a point, so i tried to implement your suggestion... I cam accross a problem tho as the checkbox doesn't seem to trigger it's checkchanged method once it is clicked by the user.

Here is the code:

private void chkTerms_CheckedChanged(object sender, System.EventArgs e)
{
if (chkTerms.Checked == true)
btnNext_4.Enabled = true;
else
btnNext_4.Enabled = false;
}

Have you come across this problem?

Cheers
James
 
Have you got postback enabled on the checkbox?

Or how about having a JavaScript registered on the click button? It won't then submit if the JavaScript returns false.

Craig
 
Good catch craig, autoPostBack was set to false.

cheers
James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top