c# Validation logic help required please

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

    c# Validation logic help required please

    I have some code which I call from a custom validator however I seem to have
    got the logic wrong and im having trouble figuring out how to write my code
    to get things to work the way I require. Below is the script I currently use
    and what it does along with what I would like it to do. Can someone please
    help me work how I can fix this.

    <script>
    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>

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


    MY PROBLEMS WITH THIS CODE ARE: -
    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)
  • Hugo Wetterberg

    #2
    Re: c# Validation logic help required please

    Hi Stephen,
    Try this:
    bool valid=
    (drop.value!=st ring.Empty && !(chk1.checked || chk2.checked))
    ||
    (drop.value==st ring.Empty && (chk1.checked || chk2.checked));

    The rule is: Valid if a value has been selected and no checkboxes have been
    checked, or if no value has been selected and one or both checkboxes has
    been checked.

    Is this correct?

    /Hugo

    On Tue, 16 Nov 2004 02:09:01 -0800, Stephen wrote:
    [color=blue]
    > I have some code which I call from a custom validator however I seem to have
    > got the logic wrong and im having trouble figuring out how to write my code
    > to get things to work the way I require. Below is the script I currently use
    > and what it does along with what I would like it to do. Can someone please
    > help me work how I can fix this.
    >
    > <script>
    > 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>
    >
    > <asp:CustomVali dator id="cvRepApp" runat="server" Font-Size="Medium"
    > ErrorMessage="M ust enter either or"
    > ClientValidatio nFunction="Vali dateDropDownOrC heckBox">*</asp:CustomValid ator>
    >
    >
    > MY PROBLEMS WITH THIS CODE ARE: -
    > 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)[/color]

    Comment

    • Stephen

      #3
      Re: c# Validation logic help required please

      thats great the logic is perfec, but im not totally sure how to write what
      you just did into a javascript funtion or a funtion for the code behind page.
      Do i need to declare bool, do I write this in the javascript. Could you
      please help me use your code to re-write my javascript or but it into a
      function in the code behind page.
      Thanks so much for your help

      "Hugo Wetterberg" wrote:
      [color=blue]
      > Hi Stephen,
      > Try this:
      > bool valid=
      > (drop.value!=st ring.Empty && !(chk1.checked || chk2.checked))
      > ||
      > (drop.value==st ring.Empty && (chk1.checked || chk2.checked));
      >
      > The rule is: Valid if a value has been selected and no checkboxes have been
      > checked, or if no value has been selected and one or both checkboxes has
      > been checked.
      >
      > Is this correct?
      >
      > /Hugo
      >
      > On Tue, 16 Nov 2004 02:09:01 -0800, Stephen wrote:
      >[color=green]
      > > I have some code which I call from a custom validator however I seem to have
      > > got the logic wrong and im having trouble figuring out how to write my code
      > > to get things to work the way I require. Below is the script I currently use
      > > and what it does along with what I would like it to do. Can someone please
      > > help me work how I can fix this.
      > >
      > > <script>
      > > 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>
      > >
      > > <asp:CustomVali dator id="cvRepApp" runat="server" Font-Size="Medium"
      > > ErrorMessage="M ust enter either or"
      > > ClientValidatio nFunction="Vali dateDropDownOrC heckBox">*</asp:CustomValid ator>
      > >
      > >
      > > MY PROBLEMS WITH THIS CODE ARE: -
      > > 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)[/color]
      >[/color]

      Comment

      • Hugo Wetterberg

        #4
        Re: c# Validation logic help required please

        Well it's a litte problematic to validate multiple controls in ASP.Net 1.1,
        but I belive that MS adresses this in ASP.Net 2. That means that it's
        difficult to use a standard CustomValidator .

        For the aspx-page:
        <form id="Form1" method="post" runat="server">
        <P>
        <asp:DropDownLi st id=drop runat="server">
        <asp:ListItem Selected="True" >Select a item</asp:ListItem>
        <asp:ListItem Value="Hello">H ello</asp:ListItem>
        <asp:ListItem Value="Hi">Hi</asp:ListItem>
        </asp:DropDownLis t><BR>
        <asp:CheckBox id=CheckBox1 runat="server"
        Text="Chk1"></asp:CheckBox><B R>
        <asp:CheckBox id=CheckBox2 runat="server"
        Text="Chk1"></asp:CheckBox>
        </P>
        <P>
        <asp:Button id=btnOk runat="server" Text="Ok"></asp:Button>
        <asp:CustomVali dator id=validator runat="server" ErrorMessage="E rror"
        ClientValidatio nFunction="vali date"
        ControlToValida te="drop">Error </asp:CustomValid ator>
        </P>
        </form>

        The codebehind would be like this if if you use a CustomValidator :
        private void validator_Serve rValidate(objec t source,
        ServerValidateE ventArgs args)
        {
        args.IsValid=
        (drop.SelectedV alue!=string.Em pty &&
        !(CheckBox1.Che cked || CheckBox2.Check ed))
        ||
        (drop.SelectedV alue==string.Em pty &&
        (CheckBox1.Chec ked || CheckBox2.Check ed));
        }

        JScript client side validator function would be:
        function validate(source , arguments)
        {
        var dropHasValue=do cument.getEleme ntById("drop"). value.length>0;
        var checked1=docume nt.getElementBy Id("CheckBox1") .checked;
        var checked2=docume nt.getElementBy Id("CheckBox2") .checked;

        arguments.IsVal id=
        (dropHasValue && !(checked1 || checked2))
        ||
        (!dropHasValue && (checked1 || checked2));
        }

        This will work, in a way. But the input will only be verified when the user
        makes a selection, or non-selection, in the drop-down menu.

        To validate you'll have to roll your own solution, but these snippets will
        help you get started.

        /Hugo


        On Tue, 16 Nov 2004 03:12:01 -0800, Stephen wrote:
        [color=blue]
        > thats great the logic is perfec, but im not totally sure how to write what
        > you just did into a javascript funtion or a funtion for the code behind page.
        > Do i need to declare bool, do I write this in the javascript. Could you
        > please help me use your code to re-write my javascript or but it into a
        > function in the code behind page.
        > Thanks so much for your help
        >
        > "Hugo Wetterberg" wrote:
        >[color=green]
        >> Hi Stephen,
        >> Try this:
        >> bool valid=
        >> (drop.value!=st ring.Empty && !(chk1.checked || chk2.checked))
        >>||
        >> (drop.value==st ring.Empty && (chk1.checked || chk2.checked));
        >>
        >> The rule is: Valid if a value has been selected and no checkboxes have been
        >> checked, or if no value has been selected and one or both checkboxes has
        >> been checked.
        >>
        >> Is this correct?
        >>
        >> /Hugo
        >>
        >> On Tue, 16 Nov 2004 02:09:01 -0800, Stephen wrote:
        >>[color=darkred]
        >>> I have some code which I call from a custom validator however I seem to have
        >>> got the logic wrong and im having trouble figuring out how to write my code
        >>> to get things to work the way I require. Below is the script I currently use
        >>> and what it does along with what I would like it to do. Can someone please
        >>> help me work how I can fix this.
        >>>
        >>> <script>
        >>> 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>
        >>>
        >>> <asp:CustomVali dator id="cvRepApp" runat="server" Font-Size="Medium"
        >>> ErrorMessage="M ust enter either or"
        >>> ClientValidatio nFunction="Vali dateDropDownOrC heckBox">*</asp:CustomValid ator>
        >>>
        >>>
        >>> MY PROBLEMS WITH THIS CODE ARE: -
        >>> 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)[/color]
        >>[/color][/color]

        Comment

        • Hugo Wetterberg

          #5
          Re: c# Validation logic help required please

          Hi Stephen,
          I made a mistake in the code, you must set the empty list items value to ""
          to make the code work:
          <asp:ListItem Selected="True" value="">Select a item</asp:ListItem>

          /Hugo

          On Tue, 16 Nov 2004 13:16:30 +0100, Hugo Wetterberg wrote:
          [color=blue]
          > Well it's a litte problematic to validate multiple controls in ASP.Net 1.1,
          > but I belive that MS adresses this in ASP.Net 2. That means that it's
          > difficult to use a standard CustomValidator .
          >
          > For the aspx-page:
          > <form id="Form1" method="post" runat="server">
          > <P>
          > <asp:DropDownLi st id=drop runat="server">
          > <asp:ListItem Selected="True" >Select a item</asp:ListItem>
          > <asp:ListItem Value="Hello">H ello</asp:ListItem>
          > <asp:ListItem Value="Hi">Hi</asp:ListItem>
          > </asp:DropDownLis t><BR>
          > <asp:CheckBox id=CheckBox1 runat="server"
          > Text="Chk1"></asp:CheckBox><B R>
          > <asp:CheckBox id=CheckBox2 runat="server"
          > Text="Chk1"></asp:CheckBox>
          > </P>
          > <P>
          > <asp:Button id=btnOk runat="server" Text="Ok"></asp:Button>
          > <asp:CustomVali dator id=validator runat="server" ErrorMessage="E rror"
          > ClientValidatio nFunction="vali date"
          > ControlToValida te="drop">Error </asp:CustomValid ator>
          > </P>
          > </form>
          >
          > The codebehind would be like this if if you use a CustomValidator :
          > private void validator_Serve rValidate(objec t source,
          > ServerValidateE ventArgs args)
          > {
          > args.IsValid=
          > (drop.SelectedV alue!=string.Em pty &&
          > !(CheckBox1.Che cked || CheckBox2.Check ed))
          > ||
          > (drop.SelectedV alue==string.Em pty &&
          > (CheckBox1.Chec ked || CheckBox2.Check ed));
          > }
          >
          > JScript client side validator function would be:
          > function validate(source , arguments)
          > {
          > var dropHasValue=do cument.getEleme ntById("drop"). value.length>0;
          > var checked1=docume nt.getElementBy Id("CheckBox1") .checked;
          > var checked2=docume nt.getElementBy Id("CheckBox2") .checked;
          >
          > arguments.IsVal id=
          > (dropHasValue && !(checked1 || checked2))
          > ||
          > (!dropHasValue && (checked1 || checked2));
          > }
          >
          > This will work, in a way. But the input will only be verified when the user
          > makes a selection, or non-selection, in the drop-down menu.
          >
          > To validate you'll have to roll your own solution, but these snippets will
          > help you get started.
          >
          > /Hugo
          >
          >
          > On Tue, 16 Nov 2004 03:12:01 -0800, Stephen wrote:
          >[color=green]
          >> thats great the logic is perfec, but im not totally sure how to write what
          >> you just did into a javascript funtion or a funtion for the code behind page.
          >> Do i need to declare bool, do I write this in the javascript. Could you
          >> please help me use your code to re-write my javascript or but it into a
          >> function in the code behind page.
          >> Thanks so much for your help
          >>
          >> "Hugo Wetterberg" wrote:
          >>[color=darkred]
          >>> Hi Stephen,
          >>> Try this:
          >>> bool valid=
          >>> (drop.value!=st ring.Empty && !(chk1.checked || chk2.checked))
          >>>||
          >>> (drop.value==st ring.Empty && (chk1.checked || chk2.checked));
          >>>
          >>> The rule is: Valid if a value has been selected and no checkboxes have been
          >>> checked, or if no value has been selected and one or both checkboxes has
          >>> been checked.
          >>>
          >>> Is this correct?
          >>>
          >>> /Hugo
          >>>
          >>> On Tue, 16 Nov 2004 02:09:01 -0800, Stephen wrote:
          >>>
          >>>> I have some code which I call from a custom validator however I seem to have
          >>>> got the logic wrong and im having trouble figuring out how to write my code
          >>>> to get things to work the way I require. Below is the script I currently use
          >>>> and what it does along with what I would like it to do. Can someone please
          >>>> help me work how I can fix this.
          >>>>
          >>>> <script>
          >>>> 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>
          >>>>
          >>>> <asp:CustomVali dator id="cvRepApp" runat="server" Font-Size="Medium"
          >>>> ErrorMessage="M ust enter either or"
          >>>> ClientValidatio nFunction="Vali dateDropDownOrC heckBox">*</asp:CustomValid ator>
          >>>>
          >>>>
          >>>> MY PROBLEMS WITH THIS CODE ARE: -
          >>>> 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)
          >>>[/color][/color][/color]

          Comment

          • Hans Kesting

            #6
            Re: c# Validation logic help required please

            Stephen wrote:[color=blue]
            > I have some code which I call from a custom validator however I seem
            > to have got the logic wrong and im having trouble figuring out how to
            > write my code to get things to work the way I require. Below is the
            > script I currently use and what it does along with what I would like
            > it to do. Can someone please help me work how I can fix this.
            >
            > <script>
            > 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>
            >
            > <asp:CustomVali dator id="cvRepApp" runat="server" Font-Size="Medium"
            > ErrorMessage="M ust enter either or"
            > ClientValidatio nFunction="Vali dateDropDownOrC heckBox">*</asp:CustomValid ator>
            >
            >
            > MY PROBLEMS WITH THIS CODE ARE: -
            > 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)[/color]

            This has nothing to do with C#, it's purely client-side javascript. However:

            Let's see:
            If I understand correctly, you want to have either at least one of the
            checkboxed checked, or a dropdown value selected. This means EITHER
            this should be true:
            cb1.checked || cb2.checked
            OR this should be true
            ddl.value != ''

            Then the complete test could be:
            (cb1.checked || cb2.checked) != (ddl.value != '')

            Or, to use your code:

            function ValidateDropDow nOrCheckBox(sen der, args)
            {
            args.IsValid = ( ( document.forms[0].CheckBox1.chec ked ||
            document.forms[0].CheckBox2.chec ked ) !=
            ( document.forms[0].DropDownList1. value != '') );
            }


            Hans Kesting


            Comment

            • Stephen

              #7
              Re: c# Validation logic help required please

              I managed to get the following code to work ok. Do you see any drawbacks or
              any problems with this. Would you no how to write this in a function in the
              code-behind page as I don't think they want me to use client-side scripting.

              <script language="javas cript">
              function ValidateDropDow nOrCheckBox(sen der, args)
              {
              var ddl = document.Form1. lstReps;
              var cb = document.Form1. chkAreYouLitiga nt;
              var cb1 = document.Form1. chkPersonalLiti gant;
              args.IsValid = ((ddl.selectedI ndex>0 && !(cb.checked || cb1.checked)) ||
              ddl.selectedInd ex==0 && (cb.checked || cb1.checked));
              /* The rule is: Valid if a value has been selected and no checkboxes have been
              checked, or if no value has been selected and one or both checkboxes has
              been checked. */

              }
              </script>

              <asp:CustomVali dator id="cvRepApp" runat="server" Font-Size="Medium"
              ErrorMessage="Y ou must either select a representative OR choose a small claim
              type. You cannot do both" ClientValidatio nFunction =
              "ValidateDropDo wnOrCheckBox">* </asp:CustomValid ator>

              "Hugo Wetterberg" wrote:
              [color=blue]
              > Well it's a litte problematic to validate multiple controls in ASP.Net 1.1,
              > but I belive that MS adresses this in ASP.Net 2. That means that it's
              > difficult to use a standard CustomValidator .
              >
              > For the aspx-page:
              > <form id="Form1" method="post" runat="server">
              > <P>
              > <asp:DropDownLi st id=drop runat="server">
              > <asp:ListItem Selected="True" >Select a item</asp:ListItem>
              > <asp:ListItem Value="Hello">H ello</asp:ListItem>
              > <asp:ListItem Value="Hi">Hi</asp:ListItem>
              > </asp:DropDownLis t><BR>
              > <asp:CheckBox id=CheckBox1 runat="server"
              > Text="Chk1"></asp:CheckBox><B R>
              > <asp:CheckBox id=CheckBox2 runat="server"
              > Text="Chk1"></asp:CheckBox>
              > </P>
              > <P>
              > <asp:Button id=btnOk runat="server" Text="Ok"></asp:Button>
              > <asp:CustomVali dator id=validator runat="server" ErrorMessage="E rror"
              > ClientValidatio nFunction="vali date"
              > ControlToValida te="drop">Error </asp:CustomValid ator>
              > </P>
              > </form>
              >
              > The codebehind would be like this if if you use a CustomValidator :
              > private void validator_Serve rValidate(objec t source,
              > ServerValidateE ventArgs args)
              > {
              > args.IsValid=
              > (drop.SelectedV alue!=string.Em pty &&
              > !(CheckBox1.Che cked || CheckBox2.Check ed))
              > ||
              > (drop.SelectedV alue==string.Em pty &&
              > (CheckBox1.Chec ked || CheckBox2.Check ed));
              > }
              >
              > JScript client side validator function would be:
              > function validate(source , arguments)
              > {
              > var dropHasValue=do cument.getEleme ntById("drop"). value.length>0;
              > var checked1=docume nt.getElementBy Id("CheckBox1") .checked;
              > var checked2=docume nt.getElementBy Id("CheckBox2") .checked;
              >
              > arguments.IsVal id=
              > (dropHasValue && !(checked1 || checked2))
              > ||
              > (!dropHasValue && (checked1 || checked2));
              > }
              >
              > This will work, in a way. But the input will only be verified when the user
              > makes a selection, or non-selection, in the drop-down menu.
              >
              > To validate you'll have to roll your own solution, but these snippets will
              > help you get started.
              >
              > /Hugo
              >
              >
              > On Tue, 16 Nov 2004 03:12:01 -0800, Stephen wrote:
              >[color=green]
              > > thats great the logic is perfec, but im not totally sure how to write what
              > > you just did into a javascript funtion or a funtion for the code behind page.
              > > Do i need to declare bool, do I write this in the javascript. Could you
              > > please help me use your code to re-write my javascript or but it into a
              > > function in the code behind page.
              > > Thanks so much for your help
              > >
              > > "Hugo Wetterberg" wrote:
              > >[color=darkred]
              > >> Hi Stephen,
              > >> Try this:
              > >> bool valid=
              > >> (drop.value!=st ring.Empty && !(chk1.checked || chk2.checked))
              > >>||
              > >> (drop.value==st ring.Empty && (chk1.checked || chk2.checked));
              > >>
              > >> The rule is: Valid if a value has been selected and no checkboxes have been
              > >> checked, or if no value has been selected and one or both checkboxes has
              > >> been checked.
              > >>
              > >> Is this correct?
              > >>
              > >> /Hugo
              > >>
              > >> On Tue, 16 Nov 2004 02:09:01 -0800, Stephen wrote:
              > >>
              > >>> I have some code which I call from a custom validator however I seem to have
              > >>> got the logic wrong and im having trouble figuring out how to write my code
              > >>> to get things to work the way I require. Below is the script I currently use
              > >>> and what it does along with what I would like it to do. Can someone please
              > >>> help me work how I can fix this.
              > >>>
              > >>> <script>
              > >>> 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>
              > >>>
              > >>> <asp:CustomVali dator id="cvRepApp" runat="server" Font-Size="Medium"
              > >>> ErrorMessage="M ust enter either or"
              > >>> ClientValidatio nFunction="Vali dateDropDownOrC heckBox">*</asp:CustomValid ator>
              > >>>
              > >>>
              > >>> MY PROBLEMS WITH THIS CODE ARE: -
              > >>> 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)
              > >>[/color][/color]
              >[/color]

              Comment

              • Hugo Wetterberg

                #8
                Re: c# Validation logic help required please

                Ok, here's the code for a sample page.

                /Hugo

                On Tue, 16 Nov 2004 05:29:03 -0800, Stephen wrote:
                [color=blue]
                > I managed to get the following code to work ok. Do you see any drawbacks or
                > any problems with this. Would you no how to write this in a function in the
                > code-behind page as I don't think they want me to use client-side scripting.
                >
                > <script language="javas cript">
                > function ValidateDropDow nOrCheckBox(sen der, args)
                > {
                > var ddl = document.Form1. lstReps;
                > var cb = document.Form1. chkAreYouLitiga nt;
                > var cb1 = document.Form1. chkPersonalLiti gant;
                > args.IsValid = ((ddl.selectedI ndex>0 && !(cb.checked || cb1.checked)) ||
                > ddl.selectedInd ex==0 && (cb.checked || cb1.checked));
                > /* The rule is: Valid if a value has been selected and no checkboxes have been
                > checked, or if no value has been selected and one or both checkboxes has
                > been checked. */
                >
                > }
                > </script>
                >
                > <asp:CustomVali dator id="cvRepApp" runat="server" Font-Size="Medium"
                > ErrorMessage="Y ou must either select a representative OR choose a small claim
                > type. You cannot do both" ClientValidatio nFunction =
                > "ValidateDropDo wnOrCheckBox">* </asp:CustomValid ator>
                >
                > "Hugo Wetterberg" wrote:
                >[color=green]
                >> Well it's a litte problematic to validate multiple controls in ASP.Net 1.1,
                >> but I belive that MS adresses this in ASP.Net 2. That means that it's
                >> difficult to use a standard CustomValidator .
                >>
                >> For the aspx-page:
                >> <form id="Form1" method="post" runat="server">
                >> <P>
                >> <asp:DropDownLi st id=drop runat="server">
                >> <asp:ListItem Selected="True" >Select a item</asp:ListItem>
                >> <asp:ListItem Value="Hello">H ello</asp:ListItem>
                >> <asp:ListItem Value="Hi">Hi</asp:ListItem>
                >> </asp:DropDownLis t><BR>
                >> <asp:CheckBox id=CheckBox1 runat="server"
                >> Text="Chk1"></asp:CheckBox><B R>
                >> <asp:CheckBox id=CheckBox2 runat="server"
                >> Text="Chk1"></asp:CheckBox>
                >> </P>
                >> <P>
                >> <asp:Button id=btnOk runat="server" Text="Ok"></asp:Button>
                >> <asp:CustomVali dator id=validator runat="server" ErrorMessage="E rror"
                >> ClientValidatio nFunction="vali date"
                >> ControlToValida te="drop">Error </asp:CustomValid ator>
                >> </P>
                >> </form>
                >>
                >> The codebehind would be like this if if you use a CustomValidator :
                >> private void validator_Serve rValidate(objec t source,
                >> ServerValidateE ventArgs args)
                >> {
                >> args.IsValid=
                >> (drop.SelectedV alue!=string.Em pty &&
                >> !(CheckBox1.Che cked || CheckBox2.Check ed))
                >> ||
                >> (drop.SelectedV alue==string.Em pty &&
                >> (CheckBox1.Chec ked || CheckBox2.Check ed));
                >> }
                >>
                >> JScript client side validator function would be:
                >> function validate(source , arguments)
                >> {
                >> var dropHasValue=do cument.getEleme ntById("drop"). value.length>0;
                >> var checked1=docume nt.getElementBy Id("CheckBox1") .checked;
                >> var checked2=docume nt.getElementBy Id("CheckBox2") .checked;
                >>
                >> arguments.IsVal id=
                >> (dropHasValue && !(checked1 || checked2))
                >> ||
                >> (!dropHasValue && (checked1 || checked2));
                >> }
                >>
                >> This will work, in a way. But the input will only be verified when the user
                >> makes a selection, or non-selection, in the drop-down menu.
                >>
                >> To validate you'll have to roll your own solution, but these snippets will
                >> help you get started.
                >>
                >> /Hugo
                >>
                >>
                >> On Tue, 16 Nov 2004 03:12:01 -0800, Stephen wrote:
                >>[color=darkred]
                >>> thats great the logic is perfec, but im not totally sure how to write what
                >>> you just did into a javascript funtion or a funtion for the code behind page.
                >>> Do i need to declare bool, do I write this in the javascript. Could you
                >>> please help me use your code to re-write my javascript or but it into a
                >>> function in the code behind page.
                >>> Thanks so much for your help
                >>>
                >>> "Hugo Wetterberg" wrote:
                >>>
                >>>> Hi Stephen,
                >>>> Try this:
                >>>> bool valid=
                >>>> (drop.value!=st ring.Empty && !(chk1.checked || chk2.checked))
                >>>>||
                >>>> (drop.value==st ring.Empty && (chk1.checked || chk2.checked));
                >>>>
                >>>> The rule is: Valid if a value has been selected and no checkboxes have been
                >>>> checked, or if no value has been selected and one or both checkboxes has
                >>>> been checked.
                >>>>
                >>>> Is this correct?
                >>>>
                >>>> /Hugo
                >>>>
                >>>> On Tue, 16 Nov 2004 02:09:01 -0800, Stephen wrote:
                >>>>
                >>>>> I have some code which I call from a custom validator however I seem to have
                >>>>> got the logic wrong and im having trouble figuring out how to write my code
                >>>>> to get things to work the way I require. Below is the script I currently use
                >>>>> and what it does along with what I would like it to do. Can someone please
                >>>>> help me work how I can fix this.
                >>>>>
                >>>>> <script>
                >>>>> 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>
                >>>>>
                >>>>> <asp:CustomVali dator id="cvRepApp" runat="server" Font-Size="Medium"
                >>>>> ErrorMessage="M ust enter either or"
                >>>>> ClientValidatio nFunction="Vali dateDropDownOrC heckBox">*</asp:CustomValid ator>
                >>>>>
                >>>>>
                >>>>> MY PROBLEMS WITH THIS CODE ARE: -
                >>>>> 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)
                >>>>[/color]
                >>[/color][/color]

                Comment

                Working...