Using .NET Validation Controls

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

    Using .NET Validation Controls

    I know how to use these great controls. They work very
    well. But is there a method or property I can use to set
    the focus to the field that is in error.

    For example... If I have a page with 5 text boxes on it
    and they are all reaquired fields. When my user falis to
    enter in something in field 3 and presses the Submit
    button, the Summarty validation control fires just fine
    but the focus is still on the Submit Button.

    Is there a way to utilize the RequiredFieldVa lidator or
    SummaryValidati onControl fields to set the focus to
    txtField3?

    Thanks

    Tom

  • Juan Gabriel Del Cid

    #2
    Re: Using .NET Validation Controls

    Set the focus manually to the control yourself... like this:

    // if userID (a TextBox) is not entered, or is invalid
    // set the focus to it.
    userID.Select() ;

    Hope that helps,
    -JG


    Comment

    • babylon

      #3
      Re: Using .NET Validation Controls

      Handle it in your submit button's click event....

      "Tom" <anonymous@disc ussions.microso ft.com> wrote in message
      news:051301c3a2 43$ae85d1e0$a30 1280a@phx.gbl.. .[color=blue]
      > I know how to use these great controls. They work very
      > well. But is there a method or property I can use to set
      > the focus to the field that is in error.
      >
      > For example... If I have a page with 5 text boxes on it
      > and they are all reaquired fields. When my user falis to
      > enter in something in field 3 and presses the Submit
      > button, the Summarty validation control fires just fine
      > but the focus is still on the Submit Button.
      >
      > Is there a way to utilize the RequiredFieldVa lidator or
      > SummaryValidati onControl fields to set the focus to
      > txtField3?
      >
      > Thanks
      >
      > Tom
      >[/color]


      Comment

      • Tom

        #4
        Re: Using .NET Validation Controls

        That's the problem. The submit button does not fire if
        their are validation errors.
        [color=blue]
        >-----Original Message-----
        >Handle it in your submit button's click event....
        >
        >"Tom" <anonymous@disc ussions.microso ft.com> wrote in[/color]
        message[color=blue]
        >news:051301c3a 243$ae85d1e0$a3 01280a@phx.gbl. ..[color=green]
        >> I know how to use these great controls. They work very
        >> well. But is there a method or property I can use to[/color][/color]
        set[color=blue][color=green]
        >> the focus to the field that is in error.
        >>
        >> For example... If I have a page with 5 text boxes on it
        >> and they are all reaquired fields. When my user falis[/color][/color]
        to[color=blue][color=green]
        >> enter in something in field 3 and presses the Submit
        >> button, the Summarty validation control fires just fine
        >> but the focus is still on the Submit Button.
        >>
        >> Is there a way to utilize the RequiredFieldVa lidator or
        >> SummaryValidati onControl fields to set the focus to
        >> txtField3?
        >>
        >> Thanks
        >>
        >> Tom
        >>[/color]
        >
        >
        >.
        >[/color]

        Comment

        • Tom

          #5
          Re: Using .NET Validation Controls

          The problem is where? The submit button event does not
          fire. Its like the Validation controls are a pseudo
          JavaScript, only without the bells and whistles.

          I know how to set focus, I just don't know where to put
          the code since the Submit Button's click event does not
          fire.
          Tom[color=blue]
          >-----Original Message-----
          >Set the focus manually to the control yourself... like[/color]
          this:[color=blue]
          >
          > // if userID (a TextBox) is not entered, or is invalid
          > // set the focus to it.
          > userID.Select() ;
          >
          >Hope that helps,
          >-JG
          >
          >
          >.
          >[/color]

          Comment

          • Doug

            #6
            Re: Using .NET Validation Controls

            Don't the validators fire off events themselves?

            doug

            "Tom" <anonymous@disc ussions.microso ft.com> wrote in message
            news:048c01c3a2 d4$cfde6190$a00 1280a@phx.gbl.. .[color=blue]
            > The problem is where? The submit button event does not
            > fire. Its like the Validation controls are a pseudo
            > JavaScript, only without the bells and whistles.
            >
            > I know how to set focus, I just don't know where to put
            > the code since the Submit Button's click event does not
            > fire.
            > Tom[color=green]
            > >-----Original Message-----
            > >Set the focus manually to the control yourself... like[/color]
            > this:[color=green]
            > >
            > > // if userID (a TextBox) is not entered, or is invalid
            > > // set the focus to it.
            > > userID.Select() ;
            > >
            > >Hope that helps,
            > >-JG
            > >
            > >
            > >.
            > >[/color][/color]


            Comment

            • Juan Gabriel Del Cid

              #7
              Re: Using .NET Validation Controls

              > The problem is where? The submit button event does not[color=blue]
              > fire. Its like the Validation controls are a pseudo
              > JavaScript, only without the bells and whistles.[/color]

              Oh, my. I'm so sorry, I thought this was a question about validators and
              controls on Winforms. My mistake.

              Well, I looked around the docs on ASP.Net's validators and found no way of
              doing it. You can do it with some javascript of your own, though (sample
              ..aspx page below). Just handle the onsubmit event of the form yourself
              (ASP.Net will modify it so it calls it's validation routine first) and check
              if your validators have their style.display properties set to "none". If
              there is an error, this property will be moething other than "none" and you
              then set the focus to the control.

              Hope that helps,
              -JG

              <!-- Test.aspx -->

              <%@ Page language="c#"%>
              <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
              <HTML>
              <HEAD>
              <title>Test</title>
              <meta name="GENERATOR " Content="Micros oft Visual Studio 7.0">
              <meta name="CODE_LANG UAGE" Content="C#">
              <meta name="vs_defaul tClientScript" content="JavaSc ript">
              <meta name="vs_target Schema"
              content="http://schemas.microso ft.com/intellisense/ie5">
              </HEAD>
              <body MS_POSITIONING= "FlowLayout ">
              <script language="javas cript">

              function SetFocusOnError (theForm) {
              if (emailValidator .style.display != "none" ||
              validEmailValid ator.style.disp lay != "none") {
              theForm.email.f ocus();
              theForm.email.s elect();
              return;
              }
              if (ageValidator.s tyle.display != "none" ||
              overThirteenVal idator.style.di splay != "none") {
              theForm.age.foc us();
              theForm.age.sel ect();
              return;
              }
              }

              </script>

              <form id="Test" method="post" runat="server"
              onsubmit="SetFo cusOnError(this );">

              <P>
              Email: <asp:TextBox id="email" runat="server"> </asp:TextBox>

              <asp:RequiredFi eldValidator id="emailValida tor" runat="server"
              ErrorMessage="E nter your email." Display="Dynami c"
              ControlToValida te="email"></asp:RequiredFie ldValidator>&nb sp;

              <asp:RegularExp ressionValidato r id="validEmailV alidator" runat="server"
              ErrorMessage="E nter a valid email address." Display="Dynami c"
              ControlToValida te="email"
              ValidationExpre ssion="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
              </asp:RegularExpr essionValidator > <BR>

              Age:
              <asp:TextBox id="age" runat="server"> </asp:TextBox>

              <asp:RequiredFi eldValidator id="ageValidato r" runat="server"
              ErrorMessage="E nter your age." Display="Dynami c"
              ControlToValida te="age"></asp:RequiredFie ldValidator>&nb sp;

              <asp:CompareVal idator id="overThirtee nValidator" runat="server"
              ErrorMessage="M ust be older than 13." Display="Dynami c"
              ControlToValida te="age" Type="Integer" ValueToCompare= "13"
              Operator="Great erThan"></asp:CompareVali dator><BR>

              <asp:Button id="Button1" runat="server" Text="Submit the form"
              EnableViewState ="False" OnClick="Refres hMe"></asp:Button></P>

              <p><asp:Label id="lblMsg" runat="server"> </asp:Label></p>
              </form>
              </body>
              </HTML>

              <script runat="server">
              public void RefreshMe(objec t sender, EventArgs e) {
              lblMsg.Text = "Submit accepted at: " +
              DateTime.Now.To LongTimeString( );
              }
              </script>


              Comment

              • Eelco Duinsbergen

                #8
                Re: Using .NET Validation Controls

                Hello,

                I think you should use the CustomValidator control, that control gives
                you
                the option to write your own Client-side and/or server side
                validationcode.

                Example:
                You could write a generic javascript function which checks a field
                for....whatever .

                <script language="javas cript">
                function ValidateField(s ource,arguments )
                {

                if(#do checks#)
                {
                arguments.isVal id = false; // Sets the Page.IsValid property
                to true
                source.focus();
                }
                else
                {
                arguments.isVal id = true;
                }
                }
                </script>

                - add a CustomValidator control to your form and set the following
                properties:
                - ControlToValida te = ControlToValida te (what a suprise)
                - ClientValidatio nFunction = ValidateField


                you should also write a serverside validation function in case the
                users browser doesn't support javascript for some reason.

                I haven't tried this myself, but i think it should work. I read in the
                MSDN documentation this doesn't work for manditory checks because the
                validation function doesn't fire when te control is empty, but you
                could try it.

                Good luck,

                Eelco Duinsbergen

                "Tom" <anonymous@disc ussions.microso ft.com> wrote in message news:<046c01c3a 2d2$d56713c0$a0 01280a@phx.gbl> ...[color=blue]
                > That's the problem. The submit button does not fire if
                > their are validation errors.
                >[color=green]
                > >-----Original Message-----
                > >Handle it in your submit button's click event....
                > >
                > >"Tom" <anonymous@disc ussions.microso ft.com> wrote in[/color]
                > message[color=green]
                > >news:051301c3a 243$ae85d1e0$a3 01280a@phx.gbl. ..[color=darkred]
                > >> I know how to use these great controls. They work very
                > >> well. But is there a method or property I can use to[/color][/color]
                > set[color=green][color=darkred]
                > >> the focus to the field that is in error.
                > >>
                > >> For example... If I have a page with 5 text boxes on it
                > >> and they are all reaquired fields. When my user falis[/color][/color]
                > to[color=green][color=darkred]
                > >> enter in something in field 3 and presses the Submit
                > >> button, the Summarty validation control fires just fine
                > >> but the focus is still on the Submit Button.
                > >>
                > >> Is there a way to utilize the RequiredFieldVa lidator or
                > >> SummaryValidati onControl fields to set the focus to
                > >> txtField3?
                > >>
                > >> Thanks
                > >>
                > >> Tom
                > >>[/color]
                > >
                > >
                > >.
                > >[/color][/color]

                Comment

                Working...