Validating user input. Either OR case

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stephen

    Validating user input. Either OR case

    Could someone please help me with some validation. I have to write code which
    checks to see whether a dropdown list is populated with a value or a checkbox
    is checked. I want the code to run on the on-click of a button.
    My page has 1 dropdown list and two checkboxes. So I either want the user
    to choose an item from the dropdownlist OR tick a checkbox(it doesn't matter
    if they tick both checkboxes). I can't allow the user to fill in the
    dropdownlist and tick a checkbox.

    Im pretty new to coding and im not sure what is the best way to do this. Can
    I use a custom validator or would I be better putting it on the on-click
    event of the button on the code behind page. I would also like the error
    messages to appear in my Error Summary which is in a panel at the bottom of
    my page.

    This is the event ill probably need to place my code
    private void Button1_Click(o bject sender, System.EventArg s e)
    {
    //Validate whether a item is selected in dropdown list OR a one/both
    checkboxes is checked. Cant allow user to check a checkbox and choose an item
    from the dropdown list
    }

    Could someone please help me write this code, im really not sure at all.

    Here is the html code. for the page im using.

    <table style="WIDTH: 305px; HEIGHT: 102px" height="102" width="305"
    border="2">
    <tr>
    <td style="HEIGHT: 31px"><asp:drop downlist id="DropDownLis t1" runat="server"
    Width="231px">
    <asp:ListItem ></asp:ListItem>
    <asp:ListItem Value="Green">G reen</asp:ListItem>
    <asp:ListItem Value="Blue">Bl ue</asp:ListItem>
    <asp:ListItem Value="Yellow"> Yellow</asp:ListItem>
    <asp:ListItem ></asp:ListItem>
    </asp:dropdownlis t></td>
    </tr>
    <tr>
    <td style="HEIGHT: 31px"><asp:chec kbox id="CheckBox2" runat="server"
    Text="No Colour"></asp:checkbox>
    <asp:checkbox id="CheckBox1" runat="server" Text="Any Colour"></asp:checkbox>
    </td>
    </tr>
    <TR>
    <TD><asp:butt on id="Button1" runat="server" Text="Button"></asp:button></TD>
    </TR>
    <tr><td>
    <asp:panel id="pnMessagePa nel" runat="server" Height="45px" Width="98.13%">
    <asp:Validation Summary id="vsSummaryOf Errors" runat="server" Width="897px"
    Height="8px" BackColor="#fff fcc">
    </asp:ValidationS ummary>
    </asp:panel>
    </td></tr>
    </table>

    Thanks very much for any help anyone can give me
  • Dan  Bass

    #2
    Re: Validating user input. Either OR case

    Stephen,

    One point of clarification, are you refering to an object being selected in
    your dropdownlist which is not null?
    Note that this is different to whether the dropdownlist is populated with
    items...

    If so, then client-side validation would be one way to go.

    Write something like the following in the HTML <Head> tags

    <script>

    function ValidateDropDow nOrCheckBox(sen der, args)
    {
    if ( document.forms[0].DropDownList1. value ||
    && document.forms[0].CheckBox1.chec ked )
    {
    args.IsValid = true;
    return;
    }
    args.IsValid = false;
    }

    </script>


    Then what you need to do is create a custom validator on your page (which
    would be the error message you want to display), and reference our
    javascript function by populating the "ClientValidati onFunction" with
    "ValidateDropDo wnOrCheckBox"

    This will not let the user click on any post back button until the
    args.IsValid is set to true in the javascript function.


    Good luck.

    Dan.


    "Stephen" <Stephen@discus sions.microsoft .com> wrote in message
    news:9B1DA0ED-9D71-499C-9E30-94DA4776E57F@mi crosoft.com...[color=blue]
    > Could someone please help me with some validation. I have to write code
    > which
    > checks to see whether a dropdown list is populated with a value or a
    > checkbox
    > is checked. I want the code to run on the on-click of a button.
    > My page has 1 dropdown list and two checkboxes. So I either want the user
    > to choose an item from the dropdownlist OR tick a checkbox(it doesn't
    > matter
    > if they tick both checkboxes). I can't allow the user to fill in the
    > dropdownlist and tick a checkbox.
    >
    > Im pretty new to coding and im not sure what is the best way to do this.
    > Can
    > I use a custom validator or would I be better putting it on the on-click
    > event of the button on the code behind page. I would also like the error
    > messages to appear in my Error Summary which is in a panel at the bottom
    > of
    > my page.
    >
    > This is the event ill probably need to place my code
    > private void Button1_Click(o bject sender, System.EventArg s e)
    > {
    > //Validate whether a item is selected in dropdown list OR a one/both
    > checkboxes is checked. Cant allow user to check a checkbox and choose an
    > item
    > from the dropdown list
    > }
    >
    > Could someone please help me write this code, im really not sure at all.
    >
    > Here is the html code. for the page im using.
    >
    > <table style="WIDTH: 305px; HEIGHT: 102px" height="102" width="305"
    > border="2">
    > <tr>
    > <td style="HEIGHT: 31px"><asp:drop downlist id="DropDownLis t1"
    > runat="server"
    > Width="231px">
    > <asp:ListItem ></asp:ListItem>
    > <asp:ListItem Value="Green">G reen</asp:ListItem>
    > <asp:ListItem Value="Blue">Bl ue</asp:ListItem>
    > <asp:ListItem Value="Yellow"> Yellow</asp:ListItem>
    > <asp:ListItem ></asp:ListItem>
    > </asp:dropdownlis t></td>
    > </tr>
    > <tr>
    > <td style="HEIGHT: 31px"><asp:chec kbox id="CheckBox2" runat="server"
    > Text="No Colour"></asp:checkbox>
    > <asp:checkbox id="CheckBox1" runat="server" Text="Any
    > Colour"></asp:checkbox>
    > </td>
    > </tr>
    > <TR>
    > <TD><asp:butt on id="Button1" runat="server"
    > Text="Button"></asp:button></TD>
    > </TR>
    > <tr><td>
    > <asp:panel id="pnMessagePa nel" runat="server" Height="45px"
    > Width="98.13%">
    > <asp:Validation Summary id="vsSummaryOf Errors" runat="server" Width="897px"
    > Height="8px" BackColor="#fff fcc">
    > </asp:ValidationS ummary>
    > </asp:panel>
    > </td></tr>
    > </table>
    >
    > Thanks very much for any help anyone can give me[/color]


    Comment

    • Stephen

      #3
      Re: Validating user input. Either OR case

      I tried the following below and the script kicks in however but no matter
      what I do it won't let me move forward even if I select an item in the
      dropdown list or check checkbox2. The error message keeps appearing.

      One point of clarification, are you refering to an object being selected in
      your dropdownlist which is not null?
      Just want to check wether something has been selected. Yes then something
      which is not null. I need to change the script slightly as well so as both
      checkbox1 and checkbox2 are included. If checkbox1 &2 are checked that is ok
      as long as nothing is chosen from the dropdown list. and if an item is chosen
      from the dropdownlist then neither checkbox can be checked.

      Can you provide me with a little more help with this. I really appreciate
      your help.

      Code im using is as follows: -

      <script language=javasc ript>
      function ValidateDropDow nOrCheckBox(sen der, args)
      {
      if ( document.forms[0].DropDownList1. value ||&&
      document.forms[0].CheckBox2.chec ked )
      {
      args.IsValid = true;
      return;
      }
      args.IsValid = false;
      }
      </script>

      Onclick event of the button is: -
      protected void Button2_Click(o bject sender, System.EventArg s e)
      {
      Response.Redire ct("WebForm2.as px");
      }

      <asp:CustomVali dator id="cvRepApp" runat="server" Font-Size="Medium"
      ErrorMessage="M ust enter either or"
      ClientValidatio nFunction="Vali dateDropDownOrC heckBox">
      *</asp:CustomValid ator>


      "Dan Bass" wrote:
      [color=blue]
      > Stephen,
      >
      > One point of clarification, are you refering to an object being selected in
      > your dropdownlist which is not null?
      > Note that this is different to whether the dropdownlist is populated with
      > items...
      >
      > If so, then client-side validation would be one way to go.
      >
      > Write something like the following in the HTML <Head> tags
      >
      > <script>
      >
      > function ValidateDropDow nOrCheckBox(sen der, args)
      > {
      > if ( document.forms[0].DropDownList1. value ||
      > && document.forms[0].CheckBox1.chec ked )
      > {
      > args.IsValid = true;
      > return;
      > }
      > args.IsValid = false;
      > }
      >
      > </script>
      >
      >
      > Then what you need to do is create a custom validator on your page (which
      > would be the error message you want to display), and reference our
      > javascript function by populating the "ClientValidati onFunction" with
      > "ValidateDropDo wnOrCheckBox"
      >
      > This will not let the user click on any post back button until the
      > args.IsValid is set to true in the javascript function.
      >
      >
      > Good luck.
      >
      > Dan.
      >
      >
      > "Stephen" <Stephen@discus sions.microsoft .com> wrote in message
      > news:9B1DA0ED-9D71-499C-9E30-94DA4776E57F@mi crosoft.com...[color=green]
      > > Could someone please help me with some validation. I have to write code
      > > which
      > > checks to see whether a dropdown list is populated with a value or a
      > > checkbox
      > > is checked. I want the code to run on the on-click of a button.
      > > My page has 1 dropdown list and two checkboxes. So I either want the user
      > > to choose an item from the dropdownlist OR tick a checkbox(it doesn't
      > > matter
      > > if they tick both checkboxes). I can't allow the user to fill in the
      > > dropdownlist and tick a checkbox.
      > >
      > > Im pretty new to coding and im not sure what is the best way to do this.
      > > Can
      > > I use a custom validator or would I be better putting it on the on-click
      > > event of the button on the code behind page. I would also like the error
      > > messages to appear in my Error Summary which is in a panel at the bottom
      > > of
      > > my page.
      > >
      > > This is the event ill probably need to place my code
      > > private void Button1_Click(o bject sender, System.EventArg s e)
      > > {
      > > //Validate whether a item is selected in dropdown list OR a one/both
      > > checkboxes is checked. Cant allow user to check a checkbox and choose an
      > > item
      > > from the dropdown list
      > > }
      > >
      > > Could someone please help me write this code, im really not sure at all.
      > >
      > > Here is the html code. for the page im using.
      > >
      > > <table style="WIDTH: 305px; HEIGHT: 102px" height="102" width="305"
      > > border="2">
      > > <tr>
      > > <td style="HEIGHT: 31px"><asp:drop downlist id="DropDownLis t1"
      > > runat="server"
      > > Width="231px">
      > > <asp:ListItem ></asp:ListItem>
      > > <asp:ListItem Value="Green">G reen</asp:ListItem>
      > > <asp:ListItem Value="Blue">Bl ue</asp:ListItem>
      > > <asp:ListItem Value="Yellow"> Yellow</asp:ListItem>
      > > <asp:ListItem ></asp:ListItem>
      > > </asp:dropdownlis t></td>
      > > </tr>
      > > <tr>
      > > <td style="HEIGHT: 31px"><asp:chec kbox id="CheckBox2" runat="server"
      > > Text="No Colour"></asp:checkbox>
      > > <asp:checkbox id="CheckBox1" runat="server" Text="Any
      > > Colour"></asp:checkbox>
      > > </td>
      > > </tr>
      > > <TR>
      > > <TD><asp:butt on id="Button1" runat="server"
      > > Text="Button"></asp:button></TD>
      > > </TR>
      > > <tr><td>
      > > <asp:panel id="pnMessagePa nel" runat="server" Height="45px"
      > > Width="98.13%">
      > > <asp:Validation Summary id="vsSummaryOf Errors" runat="server" Width="897px"
      > > Height="8px" BackColor="#fff fcc">
      > > </asp:ValidationS ummary>
      > > </asp:panel>
      > > </td></tr>
      > > </table>
      > >
      > > Thanks very much for any help anyone can give me[/color]
      >
      >
      >[/color]

      Comment

      • Dan  Bass

        #4
        Re: Validating user input. Either OR case

        Stephen

        you weren't too far off...

        Try this:


        function ValidateDropDow nOrCheckBox(sen der, args)
        {
        if ( document.forms[0].DropDownList1. value ||
        ( document.forms[0].CheckBox2.chec ked &&
        document.forms[0].CheckBox1.chec ked )
        )
        {
        args.IsValid = true;
        return;
        }
        args.IsValid = false;
        }
        </script>



        "Stephen" <Stephen@discus sions.microsoft .com> wrote in message
        news:F2452DF5-0E7F-421F-B523-0F4E4E80666E@mi crosoft.com...[color=blue]
        >I tried the following below and the script kicks in however but no matter
        > what I do it won't let me move forward even if I select an item in the
        > dropdown list or check checkbox2. The error message keeps appearing.
        >
        > One point of clarification, are you refering to an object being selected
        > in
        > your dropdownlist which is not null?
        > Just want to check wether something has been selected. Yes then something
        > which is not null. I need to change the script slightly as well so as
        > both
        > checkbox1 and checkbox2 are included. If checkbox1 &2 are checked that is
        > ok
        > as long as nothing is chosen from the dropdown list. and if an item is
        > chosen
        > from the dropdownlist then neither checkbox can be checked.
        >
        > Can you provide me with a little more help with this. I really appreciate
        > your help.
        >
        > Code im using is as follows: -
        >
        > <script language=javasc ript>
        > function ValidateDropDow nOrCheckBox(sen der, args)
        > {
        > if ( document.forms[0].DropDownList1. value ||&&
        > document.forms[0].CheckBox2.chec ked )
        > {
        > args.IsValid = true;
        > return;
        > }
        > args.IsValid = false;
        > }
        > </script>
        >
        > Onclick event of the button is: -
        > protected void Button2_Click(o bject sender, System.EventArg s e)
        > {
        > Response.Redire ct("WebForm2.as px");
        > }
        >
        > <asp:CustomVali dator id="cvRepApp" runat="server" Font-Size="Medium"
        > ErrorMessage="M ust enter either or"
        > ClientValidatio nFunction="Vali dateDropDownOrC heckBox">
        > *</asp:CustomValid ator>
        >
        >
        > "Dan Bass" wrote:
        >[color=green]
        >> Stephen,
        >>
        >> One point of clarification, are you refering to an object being selected
        >> in
        >> your dropdownlist which is not null?
        >> Note that this is different to whether the dropdownlist is populated with
        >> items...
        >>
        >> If so, then client-side validation would be one way to go.
        >>
        >> Write something like the following in the HTML <Head> tags
        >>
        >> <script>
        >>
        >> function ValidateDropDow nOrCheckBox(sen der, args)
        >> {
        >> if ( document.forms[0].DropDownList1. value ||
        >> && document.forms[0].CheckBox1.chec ked )
        >> {
        >> args.IsValid = true;
        >> return;
        >> }
        >> args.IsValid = false;
        >> }
        >>
        >> </script>
        >>
        >>
        >> Then what you need to do is create a custom validator on your page (which
        >> would be the error message you want to display), and reference our
        >> javascript function by populating the "ClientValidati onFunction" with
        >> "ValidateDropDo wnOrCheckBox"
        >>
        >> This will not let the user click on any post back button until the
        >> args.IsValid is set to true in the javascript function.
        >>
        >>
        >> Good luck.
        >>
        >> Dan.
        >>
        >>
        >> "Stephen" <Stephen@discus sions.microsoft .com> wrote in message
        >> news:9B1DA0ED-9D71-499C-9E30-94DA4776E57F@mi crosoft.com...[color=darkred]
        >> > Could someone please help me with some validation. I have to write code
        >> > which
        >> > checks to see whether a dropdown list is populated with a value or a
        >> > checkbox
        >> > is checked. I want the code to run on the on-click of a button.
        >> > My page has 1 dropdown list and two checkboxes. So I either want the
        >> > user
        >> > to choose an item from the dropdownlist OR tick a checkbox(it doesn't
        >> > matter
        >> > if they tick both checkboxes). I can't allow the user to fill in the
        >> > dropdownlist and tick a checkbox.
        >> >
        >> > Im pretty new to coding and im not sure what is the best way to do
        >> > this.
        >> > Can
        >> > I use a custom validator or would I be better putting it on the
        >> > on-click
        >> > event of the button on the code behind page. I would also like the
        >> > error
        >> > messages to appear in my Error Summary which is in a panel at the
        >> > bottom
        >> > of
        >> > my page.
        >> >
        >> > This is the event ill probably need to place my code
        >> > private void Button1_Click(o bject sender, System.EventArg s e)
        >> > {
        >> > //Validate whether a item is selected in dropdown list OR a one/both
        >> > checkboxes is checked. Cant allow user to check a checkbox and choose
        >> > an
        >> > item
        >> > from the dropdown list
        >> > }
        >> >
        >> > Could someone please help me write this code, im really not sure at
        >> > all.
        >> >
        >> > Here is the html code. for the page im using.
        >> >
        >> > <table style="WIDTH: 305px; HEIGHT: 102px" height="102" width="305"
        >> > border="2">
        >> > <tr>
        >> > <td style="HEIGHT: 31px"><asp:drop downlist id="DropDownLis t1"
        >> > runat="server"
        >> > Width="231px">
        >> > <asp:ListItem ></asp:ListItem>
        >> > <asp:ListItem Value="Green">G reen</asp:ListItem>
        >> > <asp:ListItem Value="Blue">Bl ue</asp:ListItem>
        >> > <asp:ListItem Value="Yellow"> Yellow</asp:ListItem>
        >> > <asp:ListItem ></asp:ListItem>
        >> > </asp:dropdownlis t></td>
        >> > </tr>
        >> > <tr>
        >> > <td style="HEIGHT: 31px"><asp:chec kbox id="CheckBox2" runat="server"
        >> > Text="No Colour"></asp:checkbox>
        >> > <asp:checkbox id="CheckBox1" runat="server" Text="Any
        >> > Colour"></asp:checkbox>
        >> > </td>
        >> > </tr>
        >> > <TR>
        >> > <TD><asp:butt on id="Button1" runat="server"
        >> > Text="Button"></asp:button></TD>
        >> > </TR>
        >> > <tr><td>
        >> > <asp:panel id="pnMessagePa nel" runat="server" Height="45px"
        >> > Width="98.13%">
        >> > <asp:Validation Summary id="vsSummaryOf Errors" runat="server"
        >> > Width="897px"
        >> > Height="8px" BackColor="#fff fcc">
        >> > </asp:ValidationS ummary>
        >> > </asp:panel>
        >> > </td></tr>
        >> > </table>
        >> >
        >> > Thanks very much for any help anyone can give me[/color]
        >>
        >>
        >>[/color][/color]


        Comment

        • Stephen

          #5
          Re: Validating user input. Either OR case

          Sorry but this still doesn't seem to work.
          At present the page is redirected if both checkboxes are checked and
          something is selected in the dropdown list. This needs to show an error
          Also the page shows an error if ONLY one checkbox is checked AND nothing is
          selected in the dropdown list. I want this sequence to be allowed.
          It also allows the page to redirect if one checkbox is checked and something
          is selected in the dropdown list. This need to show an error.

          I need my logic to work like so.
          If I tick one of the checkboxes (and nothing is selected in the
          dropdownlist) I want the arg to be TRUE (in other words allow page event to
          run)
          If I tick both of the checkboxes (and nothing is selected in the
          dropdownlist) I want the arg to be TRUE (in other words allow page event to
          run)
          If neither checkbox is checked but something is selected in the dropdownlist
          then I want the arg to be TRUE (in other words allow page event to run)
          If niether checkbox is checked and nothing is selected in the dropdownlist
          then i want the arg to be FALSE (in other words SHOW ERROR MESSAGE)

          "Dan Bass" wrote:
          [color=blue]
          > Stephen
          >
          > you weren't too far off...
          >
          > Try this:
          >
          >
          > function ValidateDropDow nOrCheckBox(sen der, args)
          > {
          > if ( document.forms[0].DropDownList1. value ||
          > ( document.forms[0].CheckBox2.chec ked &&
          > document.forms[0].CheckBox1.chec ked )
          > )
          > {
          > args.IsValid = true;
          > return;
          > }
          > args.IsValid = false;
          > }
          > </script>
          >
          >
          >
          > "Stephen" <Stephen@discus sions.microsoft .com> wrote in message
          > news:F2452DF5-0E7F-421F-B523-0F4E4E80666E@mi crosoft.com...[color=green]
          > >I tried the following below and the script kicks in however but no matter
          > > what I do it won't let me move forward even if I select an item in the
          > > dropdown list or check checkbox2. The error message keeps appearing.
          > >
          > > One point of clarification, are you refering to an object being selected
          > > in
          > > your dropdownlist which is not null?
          > > Just want to check wether something has been selected. Yes then something
          > > which is not null. I need to change the script slightly as well so as
          > > both
          > > checkbox1 and checkbox2 are included. If checkbox1 &2 are checked that is
          > > ok
          > > as long as nothing is chosen from the dropdown list. and if an item is
          > > chosen
          > > from the dropdownlist then neither checkbox can be checked.
          > >
          > > Can you provide me with a little more help with this. I really appreciate
          > > your help.
          > >
          > > Code im using is as follows: -
          > >
          > > <script language=javasc ript>
          > > function ValidateDropDow nOrCheckBox(sen der, args)
          > > {
          > > if ( document.forms[0].DropDownList1. value ||&&
          > > document.forms[0].CheckBox2.chec ked )
          > > {
          > > args.IsValid = true;
          > > return;
          > > }
          > > args.IsValid = false;
          > > }
          > > </script>
          > >
          > > Onclick event of the button is: -
          > > protected void Button2_Click(o bject sender, System.EventArg s e)
          > > {
          > > Response.Redire ct("WebForm2.as px");
          > > }
          > >
          > > <asp:CustomVali dator id="cvRepApp" runat="server" Font-Size="Medium"
          > > ErrorMessage="M ust enter either or"
          > > ClientValidatio nFunction="Vali dateDropDownOrC heckBox">
          > > *</asp:CustomValid ator>
          > >
          > >
          > > "Dan Bass" wrote:
          > >[color=darkred]
          > >> Stephen,
          > >>
          > >> One point of clarification, are you refering to an object being selected
          > >> in
          > >> your dropdownlist which is not null?
          > >> Note that this is different to whether the dropdownlist is populated with
          > >> items...
          > >>
          > >> If so, then client-side validation would be one way to go.
          > >>
          > >> Write something like the following in the HTML <Head> tags
          > >>
          > >> <script>
          > >>
          > >> function ValidateDropDow nOrCheckBox(sen der, args)
          > >> {
          > >> if ( document.forms[0].DropDownList1. value ||
          > >> && document.forms[0].CheckBox1.chec ked )
          > >> {
          > >> args.IsValid = true;
          > >> return;
          > >> }
          > >> args.IsValid = false;
          > >> }
          > >>
          > >> </script>
          > >>
          > >>
          > >> Then what you need to do is create a custom validator on your page (which
          > >> would be the error message you want to display), and reference our
          > >> javascript function by populating the "ClientValidati onFunction" with
          > >> "ValidateDropDo wnOrCheckBox"
          > >>
          > >> This will not let the user click on any post back button until the
          > >> args.IsValid is set to true in the javascript function.
          > >>
          > >>
          > >> Good luck.
          > >>
          > >> Dan.
          > >>
          > >>
          > >> "Stephen" <Stephen@discus sions.microsoft .com> wrote in message
          > >> news:9B1DA0ED-9D71-499C-9E30-94DA4776E57F@mi crosoft.com...
          > >> > Could someone please help me with some validation. I have to write code
          > >> > which
          > >> > checks to see whether a dropdown list is populated with a value or a
          > >> > checkbox
          > >> > is checked. I want the code to run on the on-click of a button.
          > >> > My page has 1 dropdown list and two checkboxes. So I either want the
          > >> > user
          > >> > to choose an item from the dropdownlist OR tick a checkbox(it doesn't
          > >> > matter
          > >> > if they tick both checkboxes). I can't allow the user to fill in the
          > >> > dropdownlist and tick a checkbox.
          > >> >
          > >> > Im pretty new to coding and im not sure what is the best way to do
          > >> > this.
          > >> > Can
          > >> > I use a custom validator or would I be better putting it on the
          > >> > on-click
          > >> > event of the button on the code behind page. I would also like the
          > >> > error
          > >> > messages to appear in my Error Summary which is in a panel at the
          > >> > bottom
          > >> > of
          > >> > my page.
          > >> >
          > >> > This is the event ill probably need to place my code
          > >> > private void Button1_Click(o bject sender, System.EventArg s e)
          > >> > {
          > >> > //Validate whether a item is selected in dropdown list OR a one/both
          > >> > checkboxes is checked. Cant allow user to check a checkbox and choose
          > >> > an
          > >> > item
          > >> > from the dropdown list
          > >> > }
          > >> >
          > >> > Could someone please help me write this code, im really not sure at
          > >> > all.
          > >> >
          > >> > Here is the html code. for the page im using.
          > >> >
          > >> > <table style="WIDTH: 305px; HEIGHT: 102px" height="102" width="305"
          > >> > border="2">
          > >> > <tr>
          > >> > <td style="HEIGHT: 31px"><asp:drop downlist id="DropDownLis t1"
          > >> > runat="server"
          > >> > Width="231px">
          > >> > <asp:ListItem ></asp:ListItem>
          > >> > <asp:ListItem Value="Green">G reen</asp:ListItem>
          > >> > <asp:ListItem Value="Blue">Bl ue</asp:ListItem>
          > >> > <asp:ListItem Value="Yellow"> Yellow</asp:ListItem>
          > >> > <asp:ListItem ></asp:ListItem>
          > >> > </asp:dropdownlis t></td>
          > >> > </tr>
          > >> > <tr>
          > >> > <td style="HEIGHT: 31px"><asp:chec kbox id="CheckBox2" runat="server"
          > >> > Text="No Colour"></asp:checkbox>
          > >> > <asp:checkbox id="CheckBox1" runat="server" Text="Any
          > >> > Colour"></asp:checkbox>
          > >> > </td>
          > >> > </tr>
          > >> > <TR>
          > >> > <TD><asp:butt on id="Button1" runat="server"
          > >> > Text="Button"></asp:button></TD>
          > >> > </TR>
          > >> > <tr><td>
          > >> > <asp:panel id="pnMessagePa nel" runat="server" Height="45px"
          > >> > Width="98.13%">
          > >> > <asp:Validation Summary id="vsSummaryOf Errors" runat="server"
          > >> > Width="897px"
          > >> > Height="8px" BackColor="#fff fcc">
          > >> > </asp:ValidationS ummary>
          > >> > </asp:panel>
          > >> > </td></tr>
          > >> > </table>
          > >> >
          > >> > Thanks very much for any help anyone can give me
          > >>
          > >>
          > >>[/color][/color]
          >
          >
          >[/color]

          Comment

          • Dan  Bass

            #6
            Re: Validating user input. Either OR case

            Stephen,

            That's a bit clearer, the introduction of a second check box confused me as
            to what you were trying to do.

            function ValidateDropDow nOrCheckBox(sen der, args)
            {
            // if either/box checkbox is selected, and the dropdownlist has no
            selection
            if ( ( document.forms[0].CheckBox2.chec ked ||
            document.forms[0].CheckBox1.chec ked) &&
            document.forms[0].DropDownList1. value == null )
            {
            args.IsValid = true;
            return;
            }

            // neither box is checked, but there is a dropdown selection
            if ( !document.forms[0].CheckBox2.chec ked &&
            !document.forms[0].CheckBox1.chec ked &&
            document.forms[0].DropDownList1. value )
            {
            args.IsValid = true;
            return;
            }

            // we get here if:
            // - either/both check boxes are checked and there is a selection
            // - nothing is checked and nothing is selected
            // the above could be shorten by XOR, but this wouldn't be as
            readable
            // and would be more difficult to follow
            args.IsValid = false;
            }
            </script>



            "Stephen" <Stephen@discus sions.microsoft .com> wrote in message
            news:BB1B901E-D6AF-42F7-B620-72498345604B@mi crosoft.com...[color=blue]
            > Sorry but this still doesn't seem to work.
            > At present the page is redirected if both checkboxes are checked and
            > something is selected in the dropdown list. This needs to show an error
            > Also the page shows an error if ONLY one checkbox is checked AND nothing
            > is
            > selected in the dropdown list. I want this sequence to be allowed.
            > It also allows the page to redirect if one checkbox is checked and
            > something
            > is selected in the dropdown list. This need to show an error.
            >
            > I need my logic to work like so.
            > If I tick one of the checkboxes (and nothing is selected in the
            > dropdownlist) I want the arg to be TRUE (in other words allow page event
            > to
            > run)
            > If I tick both of the checkboxes (and nothing is selected in the
            > dropdownlist) I want the arg to be TRUE (in other words allow page event
            > to
            > run)
            > If neither checkbox is checked but something is selected in the
            > dropdownlist
            > then I want the arg to be TRUE (in other words allow page event to run)
            > If niether checkbox is checked and nothing is selected in the dropdownlist
            > then i want the arg to be FALSE (in other words SHOW ERROR MESSAGE)
            >
            > "Dan Bass" wrote:
            >[color=green]
            >> Stephen
            >>
            >> you weren't too far off...
            >>
            >> Try this:
            >>
            >>
            >> function ValidateDropDow nOrCheckBox(sen der, args)
            >> {
            >> if ( document.forms[0].DropDownList1. value ||
            >> ( document.forms[0].CheckBox2.chec ked &&
            >> document.forms[0].CheckBox1.chec ked )
            >> )
            >> {
            >> args.IsValid = true;
            >> return;
            >> }
            >> args.IsValid = false;
            >> }
            >> </script>
            >>
            >>
            >>
            >> "Stephen" <Stephen@discus sions.microsoft .com> wrote in message
            >> news:F2452DF5-0E7F-421F-B523-0F4E4E80666E@mi crosoft.com...[color=darkred]
            >> >I tried the following below and the script kicks in however but no
            >> >matter
            >> > what I do it won't let me move forward even if I select an item in the
            >> > dropdown list or check checkbox2. The error message keeps appearing.
            >> >
            >> > One point of clarification, are you refering to an object being
            >> > selected
            >> > in
            >> > your dropdownlist which is not null?
            >> > Just want to check wether something has been selected. Yes then
            >> > something
            >> > which is not null. I need to change the script slightly as well so as
            >> > both
            >> > checkbox1 and checkbox2 are included. If checkbox1 &2 are checked that
            >> > is
            >> > ok
            >> > as long as nothing is chosen from the dropdown list. and if an item is
            >> > chosen
            >> > from the dropdownlist then neither checkbox can be checked.
            >> >
            >> > Can you provide me with a little more help with this. I really
            >> > appreciate
            >> > your help.
            >> >
            >> > Code im using is as follows: -
            >> >
            >> > <script language=javasc ript>
            >> > function ValidateDropDow nOrCheckBox(sen der, args)
            >> > {
            >> > if ( document.forms[0].DropDownList1. value ||&&
            >> > document.forms[0].CheckBox2.chec ked )
            >> > {
            >> > args.IsValid = true;
            >> > return;
            >> > }
            >> > args.IsValid = false;
            >> > }
            >> > </script>
            >> >
            >> > Onclick event of the button is: -
            >> > protected void Button2_Click(o bject sender, System.EventArg s e)
            >> > {
            >> > Response.Redire ct("WebForm2.as px");
            >> > }
            >> >
            >> > <asp:CustomVali dator id="cvRepApp" runat="server" Font-Size="Medium"
            >> > ErrorMessage="M ust enter either or"
            >> > ClientValidatio nFunction="Vali dateDropDownOrC heckBox">
            >> > *</asp:CustomValid ator>
            >> >
            >> >
            >> > "Dan Bass" wrote:
            >> >
            >> >> Stephen,
            >> >>
            >> >> One point of clarification, are you refering to an object being
            >> >> selected
            >> >> in
            >> >> your dropdownlist which is not null?
            >> >> Note that this is different to whether the dropdownlist is populated
            >> >> with
            >> >> items...
            >> >>
            >> >> If so, then client-side validation would be one way to go.
            >> >>
            >> >> Write something like the following in the HTML <Head> tags
            >> >>
            >> >> <script>
            >> >>
            >> >> function ValidateDropDow nOrCheckBox(sen der, args)
            >> >> {
            >> >> if ( document.forms[0].DropDownList1. value ||
            >> >> && document.forms[0].CheckBox1.chec ked )
            >> >> {
            >> >> args.IsValid = true;
            >> >> return;
            >> >> }
            >> >> args.IsValid = false;
            >> >> }
            >> >>
            >> >> </script>
            >> >>
            >> >>
            >> >> Then what you need to do is create a custom validator on your page
            >> >> (which
            >> >> would be the error message you want to display), and reference our
            >> >> javascript function by populating the "ClientValidati onFunction" with
            >> >> "ValidateDropDo wnOrCheckBox"
            >> >>
            >> >> This will not let the user click on any post back button until the
            >> >> args.IsValid is set to true in the javascript function.
            >> >>
            >> >>
            >> >> Good luck.
            >> >>
            >> >> Dan.
            >> >>
            >> >>
            >> >> "Stephen" <Stephen@discus sions.microsoft .com> wrote in message
            >> >> news:9B1DA0ED-9D71-499C-9E30-94DA4776E57F@mi crosoft.com...
            >> >> > Could someone please help me with some validation. I have to write
            >> >> > code
            >> >> > which
            >> >> > checks to see whether a dropdown list is populated with a value or a
            >> >> > checkbox
            >> >> > is checked. I want the code to run on the on-click of a button.
            >> >> > My page has 1 dropdown list and two checkboxes. So I either want
            >> >> > the
            >> >> > user
            >> >> > to choose an item from the dropdownlist OR tick a checkbox(it
            >> >> > doesn't
            >> >> > matter
            >> >> > if they tick both checkboxes). I can't allow the user to fill in
            >> >> > the
            >> >> > dropdownlist and tick a checkbox.
            >> >> >
            >> >> > Im pretty new to coding and im not sure what is the best way to do
            >> >> > this.
            >> >> > Can
            >> >> > I use a custom validator or would I be better putting it on the
            >> >> > on-click
            >> >> > event of the button on the code behind page. I would also like the
            >> >> > error
            >> >> > messages to appear in my Error Summary which is in a panel at the
            >> >> > bottom
            >> >> > of
            >> >> > my page.
            >> >> >
            >> >> > This is the event ill probably need to place my code
            >> >> > private void Button1_Click(o bject sender, System.EventArg s e)
            >> >> > {
            >> >> > //Validate whether a item is selected in dropdown list OR a one/both
            >> >> > checkboxes is checked. Cant allow user to check a checkbox and
            >> >> > choose
            >> >> > an
            >> >> > item
            >> >> > from the dropdown list
            >> >> > }
            >> >> >
            >> >> > Could someone please help me write this code, im really not sure at
            >> >> > all.
            >> >> >
            >> >> > Here is the html code. for the page im using.
            >> >> >
            >> >> > <table style="WIDTH: 305px; HEIGHT: 102px" height="102" width="305"
            >> >> > border="2">
            >> >> > <tr>
            >> >> > <td style="HEIGHT: 31px"><asp:drop downlist id="DropDownLis t1"
            >> >> > runat="server"
            >> >> > Width="231px">
            >> >> > <asp:ListItem ></asp:ListItem>
            >> >> > <asp:ListItem Value="Green">G reen</asp:ListItem>
            >> >> > <asp:ListItem Value="Blue">Bl ue</asp:ListItem>
            >> >> > <asp:ListItem Value="Yellow"> Yellow</asp:ListItem>
            >> >> > <asp:ListItem ></asp:ListItem>
            >> >> > </asp:dropdownlis t></td>
            >> >> > </tr>
            >> >> > <tr>
            >> >> > <td style="HEIGHT: 31px"><asp:chec kbox id="CheckBox2" runat="server"
            >> >> > Text="No Colour"></asp:checkbox>
            >> >> > <asp:checkbox id="CheckBox1" runat="server" Text="Any
            >> >> > Colour"></asp:checkbox>
            >> >> > </td>
            >> >> > </tr>
            >> >> > <TR>
            >> >> > <TD><asp:butt on id="Button1" runat="server"
            >> >> > Text="Button"></asp:button></TD>
            >> >> > </TR>
            >> >> > <tr><td>
            >> >> > <asp:panel id="pnMessagePa nel" runat="server" Height="45px"
            >> >> > Width="98.13%">
            >> >> > <asp:Validation Summary id="vsSummaryOf Errors" runat="server"
            >> >> > Width="897px"
            >> >> > Height="8px" BackColor="#fff fcc">
            >> >> > </asp:ValidationS ummary>
            >> >> > </asp:panel>
            >> >> > </td></tr>
            >> >> > </table>
            >> >> >
            >> >> > Thanks very much for any help anyone can give me
            >> >>
            >> >>
            >> >>[/color]
            >>
            >>
            >>[/color][/color]


            Comment

            Working...