Check blank text box using custom validator

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

    Check blank text box using custom validator

    Hi there

    I’m simply trying to check for a blank or empty value in a textbox on my
    webform. In this instance I do not want to use a requiredfieldva lidator,
    I want to use a customvalidator (as I have other code within the
    customvalidator which works).

    This won’t work (within servervalidate) :

    If txtBox.Text.ToS tring = "" Then raise exception…..

    Nor will this

    If txtBox.Text = "" Then raise exception…

    I’ve tried null (perhaps I didn’t do that correctly) but is doesn’t seem
    to work either, any ideas?

    Seems a really simple thing to do (but /sigh! I just can't do it).

    Thanks

    Alex

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Teemu Keiski

    #2
    Re: Check blank text box using custom validator

    Hi,

    show bit more the code you have. Are you sure the txtBox refers correctly to
    the TextBox?

    If you need more background info about ASp.NET validation (also custom
    validators) see this article:


    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist



    "Alex Shirley" <postings@alexs hirley.com> wrote in message
    news:OU$91hlaEH A.1356@TK2MSFTN GP09.phx.gbl...[color=blue]
    > Hi there
    >
    > I'm simply trying to check for a blank or empty value in a textbox on my
    > webform. In this instance I do not want to use a requiredfieldva lidator,
    > I want to use a customvalidator (as I have other code within the
    > customvalidator which works).
    >
    > This won't work (within servervalidate) :
    >
    > If txtBox.Text.ToS tring = "" Then raise exception...
    >
    > Nor will this
    >
    > If txtBox.Text = "" Then raise exception.
    >
    > I've tried null (perhaps I didn't do that correctly) but is doesn't seem
    > to work either, any ideas?
    >
    > Seems a really simple thing to do (but /sigh! I just can't do it).
    >
    > Thanks
    >
    > Alex
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it![/color]


    Comment

    • Mike Smith

      #3
      Re: Check blank text box using custom validator

      u raising an exception ?
      with the customvalidator , as long as u link the controltovalida te property
      u just need to code the login of when the condition is valid.. the
      serverValidate event has an args parameter,, u need to set it to either true
      or false/

      If txtBox.Text.ToS tring="" then
      args.IsValid=fa lse
      else
      args.IsValid=tr ue
      end if

      u can add the same in a client side script also and link the
      clientvalidatio n function property...

      "Alex Shirley" <postings@alexs hirley.com> wrote in message
      news:OU$91hlaEH A.1356@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Hi there
      >
      > I'm simply trying to check for a blank or empty value in a textbox on my
      > webform. In this instance I do not want to use a requiredfieldva lidator,
      > I want to use a customvalidator (as I have other code within the
      > customvalidator which works).
      >
      > This won't work (within servervalidate) :
      >
      > If txtBox.Text.ToS tring = "" Then raise exception...
      >
      > Nor will this
      >
      > If txtBox.Text = "" Then raise exception.
      >
      > I've tried null (perhaps I didn't do that correctly) but is doesn't seem
      > to work either, any ideas?
      >
      > Seems a really simple thing to do (but /sigh! I just can't do it).
      >
      > Thanks
      >
      > Alex
      >
      > *** Sent via Developersdex http://www.developersdex.com ***
      > Don't just participate in USENET...get rewarded for it![/color]


      ---
      Outgoing mail is certified Virus Free. (well i'd like to think it is.. )
      Checked by AVG anti-virus system (http://www.grisoft.com).
      Version: 6.0.720 / Virus Database: 476 - Release Date: 14/07/2004


      Comment

      • DujHoD

        #4
        Re: Check blank text box using custom validator

        For a CustomValidator , you don't want to raise an exception; you want
        to modify the ServerValidateE ventArgs that's passed in, like this:

        Sub ValidateTextBox (sender as Object, args as
        ServerValidateE ventArgs)
        Dim valid As Boolean
        valid = (txtBox.Text.Le ngth > 0)
        ' Do other stuff here...
        args.IsValid = valid
        End Sub

        Instead of txtBox.Text, you could also use args.Value, which would
        arguably make your code more maintainable:

        valid = (args.Value.Len gth > 0)

        Clent-side validation works in much the same way. Is this what you're
        asking about?


        Alex Shirley <postings@alexs hirley.com> wrote in message news:<OU$91hlaE HA.1356@TK2MSFT NGP09.phx.gbl>. ..[color=blue]
        > Hi there
        >
        > I?m simply trying to check for a blank or empty value in a textbox on my
        > webform. In this instance I do not want to use a requiredfieldva lidator,
        > I want to use a customvalidator (as I have other code within the
        > customvalidator which works).
        >
        > This won?t work (within servervalidate) :
        >
        > If txtBox.Text.ToS tring = "" Then raise exception?..
        >
        > Nor will this
        >
        > If txtBox.Text = "" Then raise exception?
        >
        > I?ve tried null (perhaps I didn?t do that correctly) but is doesn?t seem
        > to work either, any ideas?
        >
        > Seems a really simple thing to do (but /sigh! I just can't do it).[/color]

        Comment

        • Alex Shirley

          #5
          Re: Check blank text box using custom validator

          Thank for this

          My initial impressions from your posts are that I'm doing everything
          right (which is probably flawed!).

          I think I'd better look at this tomorrow under a fresh perspective and
          will provide some feedback.

          Thanks

          Alex


          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • Alex Shirley

            #6
            Re: Check blank text box using custom validator

            Weird, any ideas with this ....?

            -----------------------------------------------------------
            This code works
            -----------------------------------------------------------

            'If user types hello, the custom validator flags up
            Private Sub cvProductStartD ate_ServerValid ate(ByVal source As
            System.Object, ByVal args As
            System.Web.UI.W ebControls.Serv erValidateEvent Args) Handles
            cvProductStartD ate.ServerValid ate

            If txtProductStart Date.Text.ToStr ing <> "hello" Then
            args.IsValid = True
            Else
            cvProductStartD ate.ErrorMessag e = "You typed hello"
            args.IsValid = False
            End If
            End Sub

            -----------------------------------------------------------
            This code doesn't work
            -----------------------------------------------------------

            'Checks for a blank textbox, does not work!
            Private Sub cvProductStartD ate_ServerValid ate(ByVal source As
            System.Object, ByVal args As
            System.Web.UI.W ebControls.Serv erValidateEvent Args) Handles
            cvProductStartD ate.ServerValid ate

            If txtProductStart Date.Text.ToStr ing <> "" Then
            args.IsValid = True
            Else
            cvProductStartD ate.ErrorMessag e = "Type something in"
            args.IsValid = False
            End If
            End Sub
            ---------------------------------------------------------

            Cheers!

            Alex


            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            • Mike Smith

              #7
              Re: Check blank text box using custom validator

              well i guess thats the idea why they made the requiredfield validator ?
              cause thats the only control that checks that.. and u suppose to use the
              various validation controls in conjuction with each other.. so place a
              required validator to check for blanks and then ur custom one with what ever
              logic u need to check...

              i guess thats why in the books they say the required field validator is THE
              ONLY validator that can check for blanks ? well thats the line ive read
              someplace...

              "Alex Shirley" <postings@alexs hirley.com> wrote in message
              news:up6GdSyaEH A.3016@tk2msftn gp13.phx.gbl...[color=blue]
              > Weird, any ideas with this ....?
              >
              > -----------------------------------------------------------
              > This code works
              > -----------------------------------------------------------
              >
              > 'If user types hello, the custom validator flags up
              > Private Sub cvProductStartD ate_ServerValid ate(ByVal source As
              > System.Object, ByVal args As
              > System.Web.UI.W ebControls.Serv erValidateEvent Args) Handles
              > cvProductStartD ate.ServerValid ate
              >
              > If txtProductStart Date.Text.ToStr ing <> "hello" Then
              > args.IsValid = True
              > Else
              > cvProductStartD ate.ErrorMessag e = "You typed hello"
              > args.IsValid = False
              > End If
              > End Sub
              >
              > -----------------------------------------------------------
              > This code doesn't work
              > -----------------------------------------------------------
              >
              > 'Checks for a blank textbox, does not work!
              > Private Sub cvProductStartD ate_ServerValid ate(ByVal source As
              > System.Object, ByVal args As
              > System.Web.UI.W ebControls.Serv erValidateEvent Args) Handles
              > cvProductStartD ate.ServerValid ate
              >
              > If txtProductStart Date.Text.ToStr ing <> "" Then
              > args.IsValid = True
              > Else
              > cvProductStartD ate.ErrorMessag e = "Type something in"
              > args.IsValid = False
              > End If
              > End Sub
              > ---------------------------------------------------------
              >
              > Cheers!
              >
              > Alex
              >
              >
              > *** Sent via Developersdex http://www.developersdex.com ***
              > Don't just participate in USENET...get rewarded for it![/color]


              ---
              Outgoing mail is certified Virus Free. (well i'd like to think it is.. )
              Checked by AVG anti-virus system (http://www.grisoft.com).
              Version: 6.0.720 / Virus Database: 476 - Release Date: 14/07/2004


              Comment

              • Teemu Keiski

                #8
                Re: Check blank text box using custom validator

                Hi,

                just to add that if you use CustomValidator but don't specify the
                ControlToValida te property it will fire the validation despite if control is
                empty or not (because the control is not specified, it runs the validation
                always and therefore you could validate empty controls as well with it).

                See this article:


                The behaviour is described there:
                "You can leave the ControlToValida te blank. In this mode, the server
                function always fires once per round trip and the client function always
                fires once for each attempt to submit. You can use this to validate controls
                that cannot otherwise be validated, such as a CheckBoxList or stand-alone
                radio buttons. It can also be useful when the condition is based on multiple
                controls and you don't want it evaluated as the user tabs between fields on
                the page"

                But the result is that you can also use it for blank controls...

                --
                Teemu Keiski
                MCP, Microsoft MVP (ASP.NET), AspInsiders member
                ASP.NET Forum Moderator, AspAlliance Columnist



                "Mike Smith" <test@test.co m> wrote in message
                news:%23DKibXAb EHA.3692@TK2MSF TNGP09.phx.gbl. ..[color=blue]
                > well i guess thats the idea why they made the requiredfield validator ?
                > cause thats the only control that checks that.. and u suppose to use the
                > various validation controls in conjuction with each other.. so place a
                > required validator to check for blanks and then ur custom one with what[/color]
                ever[color=blue]
                > logic u need to check...
                >
                > i guess thats why in the books they say the required field validator is[/color]
                THE[color=blue]
                > ONLY validator that can check for blanks ? well thats the line ive read
                > someplace...
                >
                > "Alex Shirley" <postings@alexs hirley.com> wrote in message
                > news:up6GdSyaEH A.3016@tk2msftn gp13.phx.gbl...[color=green]
                > > Weird, any ideas with this ....?
                > >
                > > -----------------------------------------------------------
                > > This code works
                > > -----------------------------------------------------------
                > >
                > > 'If user types hello, the custom validator flags up
                > > Private Sub cvProductStartD ate_ServerValid ate(ByVal source As
                > > System.Object, ByVal args As
                > > System.Web.UI.W ebControls.Serv erValidateEvent Args) Handles
                > > cvProductStartD ate.ServerValid ate
                > >
                > > If txtProductStart Date.Text.ToStr ing <> "hello" Then
                > > args.IsValid = True
                > > Else
                > > cvProductStartD ate.ErrorMessag e = "You typed hello"
                > > args.IsValid = False
                > > End If
                > > End Sub
                > >
                > > -----------------------------------------------------------
                > > This code doesn't work
                > > -----------------------------------------------------------
                > >
                > > 'Checks for a blank textbox, does not work!
                > > Private Sub cvProductStartD ate_ServerValid ate(ByVal source As
                > > System.Object, ByVal args As
                > > System.Web.UI.W ebControls.Serv erValidateEvent Args) Handles
                > > cvProductStartD ate.ServerValid ate
                > >
                > > If txtProductStart Date.Text.ToStr ing <> "" Then
                > > args.IsValid = True
                > > Else
                > > cvProductStartD ate.ErrorMessag e = "Type something in"
                > > args.IsValid = False
                > > End If
                > > End Sub
                > > ---------------------------------------------------------
                > >
                > > Cheers!
                > >
                > > Alex
                > >
                > >
                > > *** Sent via Developersdex http://www.developersdex.com ***
                > > Don't just participate in USENET...get rewarded for it![/color]
                >
                >
                > ---
                > Outgoing mail is certified Virus Free. (well i'd like to think it is.. )
                > Checked by AVG anti-virus system (http://www.grisoft.com).
                > Version: 6.0.720 / Virus Database: 476 - Release Date: 14/07/2004
                >
                >[/color]


                Comment

                • Mike Smith

                  #9
                  Re: Check blank text box using custom validator

                  hey teemu !
                  thats a great tip ! thanks...

                  "Teemu Keiski" <joteke@aspalli ance.com> wrote in message
                  news:uVkTkIEbEH A.3512@TK2MSFTN GP12.phx.gbl...[color=blue]
                  > Hi,
                  >
                  > just to add that if you use CustomValidator but don't specify the
                  > ControlToValida te property it will fire the validation despite if control[/color]
                  is[color=blue]
                  > empty or not (because the control is not specified, it runs the validation
                  > always and therefore you could validate empty controls as well with it).
                  >
                  > See this article:
                  >[/color]
                  http://msdn.microsoft.com/library/de...pplusvalid.asp[color=blue]
                  >
                  > The behaviour is described there:
                  > "You can leave the ControlToValida te blank. In this mode, the server
                  > function always fires once per round trip and the client function always
                  > fires once for each attempt to submit. You can use this to validate[/color]
                  controls[color=blue]
                  > that cannot otherwise be validated, such as a CheckBoxList or stand-alone
                  > radio buttons. It can also be useful when the condition is based on[/color]
                  multiple[color=blue]
                  > controls and you don't want it evaluated as the user tabs between fields[/color]
                  on[color=blue]
                  > the page"
                  >
                  > But the result is that you can also use it for blank controls...
                  >
                  > --
                  > Teemu Keiski
                  > MCP, Microsoft MVP (ASP.NET), AspInsiders member
                  > ASP.NET Forum Moderator, AspAlliance Columnist
                  > http://blogs.aspadvice.com/joteke
                  >
                  >
                  > "Mike Smith" <test@test.co m> wrote in message
                  > news:%23DKibXAb EHA.3692@TK2MSF TNGP09.phx.gbl. ..[color=green]
                  > > well i guess thats the idea why they made the requiredfield validator ?
                  > > cause thats the only control that checks that.. and u suppose to use the
                  > > various validation controls in conjuction with each other.. so place a
                  > > required validator to check for blanks and then ur custom one with what[/color]
                  > ever[color=green]
                  > > logic u need to check...
                  > >
                  > > i guess thats why in the books they say the required field validator is[/color]
                  > THE[color=green]
                  > > ONLY validator that can check for blanks ? well thats the line ive read
                  > > someplace...
                  > >
                  > > "Alex Shirley" <postings@alexs hirley.com> wrote in message
                  > > news:up6GdSyaEH A.3016@tk2msftn gp13.phx.gbl...[color=darkred]
                  > > > Weird, any ideas with this ....?
                  > > >
                  > > > -----------------------------------------------------------
                  > > > This code works
                  > > > -----------------------------------------------------------
                  > > >
                  > > > 'If user types hello, the custom validator flags up
                  > > > Private Sub cvProductStartD ate_ServerValid ate(ByVal source As
                  > > > System.Object, ByVal args As
                  > > > System.Web.UI.W ebControls.Serv erValidateEvent Args) Handles
                  > > > cvProductStartD ate.ServerValid ate
                  > > >
                  > > > If txtProductStart Date.Text.ToStr ing <> "hello" Then
                  > > > args.IsValid = True
                  > > > Else
                  > > > cvProductStartD ate.ErrorMessag e = "You typed hello"
                  > > > args.IsValid = False
                  > > > End If
                  > > > End Sub
                  > > >
                  > > > -----------------------------------------------------------
                  > > > This code doesn't work
                  > > > -----------------------------------------------------------
                  > > >
                  > > > 'Checks for a blank textbox, does not work!
                  > > > Private Sub cvProductStartD ate_ServerValid ate(ByVal source As
                  > > > System.Object, ByVal args As
                  > > > System.Web.UI.W ebControls.Serv erValidateEvent Args) Handles
                  > > > cvProductStartD ate.ServerValid ate
                  > > >
                  > > > If txtProductStart Date.Text.ToStr ing <> "" Then
                  > > > args.IsValid = True
                  > > > Else
                  > > > cvProductStartD ate.ErrorMessag e = "Type something in"
                  > > > args.IsValid = False
                  > > > End If
                  > > > End Sub
                  > > > ---------------------------------------------------------
                  > > >
                  > > > Cheers!
                  > > >
                  > > > Alex
                  > > >
                  > > >
                  > > > *** Sent via Developersdex http://www.developersdex.com ***
                  > > > Don't just participate in USENET...get rewarded for it![/color]
                  > >
                  > >
                  > > ---
                  > > Outgoing mail is certified Virus Free. (well i'd like to think it is.. )
                  > > Checked by AVG anti-virus system (http://www.grisoft.com).
                  > > Version: 6.0.720 / Virus Database: 476 - Release Date: 14/07/2004
                  > >
                  > >[/color]
                  >
                  >[/color]


                  ---
                  Outgoing mail is certified Virus Free. (well i'd like to think it is.. )
                  Checked by AVG anti-virus system (http://www.grisoft.com).
                  Version: 6.0.720 / Virus Database: 476 - Release Date: 14/07/2004


                  Comment

                  • Alex Shirley

                    #10
                    Re: Check blank text box using custom validator

                    Thanks for this ...!

                    Alex

                    *** Sent via Developersdex http://www.developersdex.com ***
                    Don't just participate in USENET...get rewarded for it!

                    Comment

                    Working...