locked
CreateUserWizard Check Box validation Problem RRS feed

  • Question

  • User730446648 posted

    So I made a working bit of code (javascript) to validate a Terms Of Use checkbox.

    When the checkbox and CustomValidator are inside The "CreateUserWizard" form It won't work.

    Outside it, there's no problems. My code is:

     

    <asp:CheckBox ID="AcceptTermsCheckBox" runat="server" Text="&nbsp;I agree to the Detelli Property Network"

    Font-Size="8" ForeColor="#133792" ValidationGroup="CreateUserWizardControl" /><br />

    &nbsp;<asp:HyperLink runat="server" NavigateUrl="Terms.aspx"

    ForeColor="#133792" Font-Underline="true" ID="Terms" Font-Size="8">terms of use.</asp:HyperLink><br />

    <asp:CustomValidator ID="ValTermsCheckBox" ClientValidationFunction="AcceptTermsCheckBoxValidation"

    runat="server" ErrorMessage="Please accept Terms and Conditions." ValidationGroup="CreateUserWizardControl" OnServerValidate="ValTermsCheckBox_ServerValidate">

    </asp:CustomValidator>

    And my javascript code, that I can only place outside the "CreateUserWizard" form, is:

     

    <script language="javascript" type="text/javascript">

    function AcceptTermsCheckBoxValidation(source, args)

    {

    args.IsValid = document.getElementById('<%= AcceptTermsCheckBox.ClientID %>').checked;

    }

    </script>

    I've been trying to write some Visual Basic in the code behind that I can use,

    but it's not working, no matter what I do it pulls up the error as though the checkbox is unchecked. 

     

    Protected Sub ValTermsCheckBox_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)

    Dim AcceptTermsCheckBox As CheckBox = CreateUserWizardStep1.Controls(0).FindControl("AcceptTermsCheckBox")

    If AcceptTermsCheckBox.Checked = True Then

    args.IsValid = True

    End If

    End Sub

     

    This is really leaving me stumped, any ideas would be greatly appreciated. 

    Thank you in advance

     

    Monday, April 23, 2007 8:03 PM

Answers

  • User730446648 posted

    I have now figured it out, and for anyone having the same concern, here's what I did.

    In the end I did not use any code behind so ignore that. looking at the origianal code (javascript, checkbox, and custom validator) I discovered that the javascript could not find checkbox once it was imbeded into the "CreateUserWizard". So all I had to do was give it a proper location.

     

    <script type="text/javascript">

    function AcceptTermsCheckBoxValidation(source, args)

    {

    args.IsValid = document.getElementById('<%= CType(CreateUserWizardStep1.ContentTemplateContainer.FindControl("AcceptTermsCheckBox"), CheckBox).ClientID %>')

    if (!args.IsValid.checked)

    {

    ValTerms.Validate;

    return false;

    }

    else {

    return true;

    }

    }

    </script>

     

    <asp:CheckBox ID="AcceptTermsCheckBox" runat="server" Text="&nbsp;I agree to the Detelli Property Network"

    Font-Size="8" ForeColor="#133792" ValidationGroup="CreateUserWizardControl" /><br />

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:HyperLink runat="server" NavigateUrl="Terms.aspx"

    ForeColor="#133792" Font-Underline="true" ID="Terms" Font-Size="8">terms of use.</asp:HyperLink><br />

     

    <asp:CustomValidator ID="ValTerms" ClientValidationFunction="AcceptTermsCheckBoxValidation"

    runat="server" ErrorMessage="Please Accept Terms Of Use." ValidationGroup="CreateUserWizardControl">

    </asp:CustomValidator>

     

    I haven't tested this on anything other than IE but I'm sure it's fine.

    It took forever but it feels so good to beat that code.

    Good Luck to all.

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 25, 2007 10:50 AM
  • User730446648 posted

    You know, now that I am home from work and can look at it, I think I did have the same problem at first.

    Try this.

    <script type="text/javascript"> function AcceptTermsCheckBoxValidation(source, args)

    {

     

    var vCheckBox = document.getElementById('<%= CType(CreateUserWizardStep1.ContentTemplateContainer.FindControl("AcceptTermsCheckBox"), CheckBox).ClientID %>');if (!vCheckBox.checked)

    {

    args.IsValid =
    false;}else {args.IsValid = true;

    }

    }

    </script>

     

    And this is inside the user wizard control.

    <div id="agree_terms">

    <asp:CheckBox ID="AcceptTermsCheckBox" runat="server" Text="&nbsp;I agree to the Detelli Property Network"

    Font-Size="8" ForeColor="#133792" ValidationGroup="CreateUserWizardControl" /><br />

     

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:HyperLink runat="server" NavigateUrl="Main/Terms.aspx"

    ForeColor="#133792" Font-Underline="true" ID="Terms" Font-Size="8" Target="_blank">terms of use.</asp:HyperLink><br />

     

    <asp:CustomValidator ID="ValTerms" ClientValidationFunction="AcceptTermsCheckBoxValidation"

    runat="server" ErrorMessage="Please Accept Terms Of Use." ValidationGroup="CreateUserWizardControl">

    </asp:CustomValidator>

    </div>

    Sorry about that, it was quite a while ago that I wrote that code.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 18, 2008 9:59 PM

All replies

  • User730446648 posted

    I have now figured it out, and for anyone having the same concern, here's what I did.

    In the end I did not use any code behind so ignore that. looking at the origianal code (javascript, checkbox, and custom validator) I discovered that the javascript could not find checkbox once it was imbeded into the "CreateUserWizard". So all I had to do was give it a proper location.

     

    <script type="text/javascript">

    function AcceptTermsCheckBoxValidation(source, args)

    {

    args.IsValid = document.getElementById('<%= CType(CreateUserWizardStep1.ContentTemplateContainer.FindControl("AcceptTermsCheckBox"), CheckBox).ClientID %>')

    if (!args.IsValid.checked)

    {

    ValTerms.Validate;

    return false;

    }

    else {

    return true;

    }

    }

    </script>

     

    <asp:CheckBox ID="AcceptTermsCheckBox" runat="server" Text="&nbsp;I agree to the Detelli Property Network"

    Font-Size="8" ForeColor="#133792" ValidationGroup="CreateUserWizardControl" /><br />

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:HyperLink runat="server" NavigateUrl="Terms.aspx"

    ForeColor="#133792" Font-Underline="true" ID="Terms" Font-Size="8">terms of use.</asp:HyperLink><br />

     

    <asp:CustomValidator ID="ValTerms" ClientValidationFunction="AcceptTermsCheckBoxValidation"

    runat="server" ErrorMessage="Please Accept Terms Of Use." ValidationGroup="CreateUserWizardControl">

    </asp:CustomValidator>

     

    I haven't tested this on anything other than IE but I'm sure it's fine.

    It took forever but it feels so good to beat that code.

    Good Luck to all.

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 25, 2007 10:50 AM
  • User-1456364713 posted

    I tried to put the Javascript codes in the .js file and included the .js file in the .aspx.  I find that the object will be null (due to document.getElementById can not get the object).  Any idea?

    Thanks.

    Thursday, May 17, 2007 5:39 PM
  • User730446648 posted

    I had errors when I put it in a .js file, mainly because I'm using MasterPages, try just putting the code in your page outside of the CreateUserWizard section. If you're still having errors, post your code and errors.

    Friday, May 18, 2007 9:44 PM
  • User94323121 posted

    Kool thanks!

    This is exactly what i needed.

    But it doesn't work with Firefox  :-(

    And i need it with Firefox.

    Someone have an idea why it is not working with Firefox?

    N.B. Dont works for me with IE 6 in a .js external file.

    Thursday, January 17, 2008 11:56 PM
  • User-768391503 posted

    If youwant to debug in JavaScript it has a JavaScript cConsole. itwill tell you what the JavaScript Error is when you look at it. Also you can install the firebug extension to get even better control over the errors. [getFirebug.com]

     Eric
     

    Friday, January 18, 2008 1:04 PM
  • User-768391503 posted

    What is ValTerms.Validate;

    Does not look like valid JavaScript to me.

    Eric 

    Friday, January 18, 2008 1:06 PM
  • User94323121 posted
    It's the ID of the

    <asp:CustomValidator ID="ValTerms" ClientValidationFunction="AcceptTermsCheckBoxValidation"

     

    runat="server" ErrorMessage="Please Accept Terms Of Use." ValidationGroup="CreateUserWizardControl">

     

    </asp:CustomValidator>

    Friday, January 18, 2008 1:17 PM
  • User730446648 posted

    Check it on my site on the register page in firefox.

    I am still using the same code, and it works great.

    www.detelli.com/register.aspx

     

    Friday, January 18, 2008 8:09 PM
  • User94323121 posted

    Hummm, can you show me your actual code please...

    Is your Terms of use checkbox still inside the CreateUserWizard and your submit button?

    Thanks

    Friday, January 18, 2008 8:51 PM
  • User730446648 posted

    You know, now that I am home from work and can look at it, I think I did have the same problem at first.

    Try this.

    <script type="text/javascript"> function AcceptTermsCheckBoxValidation(source, args)

    {

     

    var vCheckBox = document.getElementById('<%= CType(CreateUserWizardStep1.ContentTemplateContainer.FindControl("AcceptTermsCheckBox"), CheckBox).ClientID %>');if (!vCheckBox.checked)

    {

    args.IsValid =
    false;}else {args.IsValid = true;

    }

    }

    </script>

     

    And this is inside the user wizard control.

    <div id="agree_terms">

    <asp:CheckBox ID="AcceptTermsCheckBox" runat="server" Text="&nbsp;I agree to the Detelli Property Network"

    Font-Size="8" ForeColor="#133792" ValidationGroup="CreateUserWizardControl" /><br />

     

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:HyperLink runat="server" NavigateUrl="Main/Terms.aspx"

    ForeColor="#133792" Font-Underline="true" ID="Terms" Font-Size="8" Target="_blank">terms of use.</asp:HyperLink><br />

     

    <asp:CustomValidator ID="ValTerms" ClientValidationFunction="AcceptTermsCheckBoxValidation"

    runat="server" ErrorMessage="Please Accept Terms Of Use." ValidationGroup="CreateUserWizardControl">

    </asp:CustomValidator>

    </div>

    Sorry about that, it was quite a while ago that I wrote that code.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 18, 2008 9:59 PM
  • User94323121 posted

    yeah!!! Now you're right  ;-)

    I just examine your page code at http://www.detelli.com/register.aspx and saw the differences in the code.

    Thanks darkknight187!

    Very appreciated!

    Friday, January 18, 2008 10:08 PM
  • User94323121 posted

    Is someone found how to put the code in an external .js file and make it working?

    Thanks

    By the way, here is a shorter function :

    <script type="text/javascript">
            function AcceptTermsCheckBoxValidation(source, args) {
                args.IsValid = document.getElementById('<%=CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("AcceptTermsCheckBox").ClientID%>').checked;
            }
        </script>

    Wednesday, May 5, 2010 11:14 PM
  • User-1886006708 posted

    I was messing with this problem for almost 2 hours. Thank you my code is finally working .

    Saturday, January 8, 2011 6:08 AM
  • User-233911284 posted

    The JavaScript cannot find the "ValTerms" object.  I keep getting an error when it calls the ValTerms.Validate

    Wednesday, February 22, 2012 11:15 AM