Custom validation

Discussion in 'ASP .Net' started by simon, May 27, 2004.

  1. simon

    simon Guest

    I have 2 <asp:textbox controls>:

    <asp:textbox id="txt1" Runat="server" ></asp:textbox>

    <asp:textbox id="txt2" Runat="server" ></asp:textbox>

    User must insert the value either in txt1 OR in txt2, so one of them
    shouldn't be empty.

    So I use custom validator:

    <asp:CustomValidator id="valPhone" Runat="server" ErrorMessage="You must
    insert text1 or text2!"
    ControlToValidate="txt2"
    Display="Dynamic"></asp:CustomValidator>

    AND server:

    Private Sub valPhone_ServerValidate(ByVal source As Object, ByVal args As
    System.Web.UI.WebControls.ServerValidateEventArgs) Handles
    valPhone.ServerValidate
    If txt1.Text = "" And txt2.Text = "" Then
    args.IsValid = False
    Else
    args.IsValid = True
    End If
    End Sub

    The server validation is fired only if there is some value in txt2 box.If
    txt2.text is empty then server validation is not fired.
    How can I solve this problem?

    I would like also to include client validation.

    Thank you,
    Simon
    simon, May 27, 2004
    #1
    1. Advertising

  2. Hi Simon,

    An empty field is a valid input for a custom validator! I think there
    is the possibility to leave the ControlToValidate field blank, but I
    haven't tried it yet.

    See:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspplusvalid.asp


    Regards


    Johannes





    On Thu, 27 May 2004 09:55:32 +0200, "simon"
    <> wrote:

    >I have 2 <asp:textbox controls>:
    >
    ><asp:textbox id="txt1" Runat="server" ></asp:textbox>
    >
    ><asp:textbox id="txt2" Runat="server" ></asp:textbox>
    >
    >User must insert the value either in txt1 OR in txt2, so one of them
    >shouldn't be empty.
    >
    >So I use custom validator:
    >
    ><asp:CustomValidator id="valPhone" Runat="server" ErrorMessage="You must
    >insert text1 or text2!"
    > ControlToValidate="txt2"
    >Display="Dynamic"></asp:CustomValidator>
    >
    >AND server:
    >
    >Private Sub valPhone_ServerValidate(ByVal source As Object, ByVal args As
    >System.Web.UI.WebControls.ServerValidateEventArgs) Handles
    >valPhone.ServerValidate
    > If txt1.Text = "" And txt2.Text = "" Then
    > args.IsValid = False
    > Else
    > args.IsValid = True
    > End If
    >End Sub
    >
    >The server validation is fired only if there is some value in txt2 box.If
    >txt2.text is empty then server validation is not fired.
    >How can I solve this problem?
    >
    >I would like also to include client validation.
    >
    >Thank you,
    >Simon
    >
    Johannes Eble, May 27, 2004
    #2
    1. Advertising

  3. simon

    DujHoD Guest

    Try getting rid of the ControlToValidate property. It's not necessary
    for a CustomValidator, and in this case, it may be what is preventing
    the ServerValidate event from firing.

    For client-side validation, use the ClientValidationFunction property
    of the CustomValidator.


    "simon" <> wrote in message news:<#>...
    > I have 2 <asp:textbox controls>:
    >
    > <asp:textbox id="txt1" Runat="server" ></asp:textbox>
    >
    > <asp:textbox id="txt2" Runat="server" ></asp:textbox>
    >
    > User must insert the value either in txt1 OR in txt2, so one of them
    > shouldn't be empty.
    >
    > So I use custom validator:
    >
    > <asp:CustomValidator id="valPhone" Runat="server" ErrorMessage="You must
    > insert text1 or text2!"
    > ControlToValidate="txt2"
    > Display="Dynamic"></asp:CustomValidator>
    >
    > AND server:
    >
    > Private Sub valPhone_ServerValidate(ByVal source As Object, ByVal args As
    > System.Web.UI.WebControls.ServerValidateEventArgs) Handles
    > valPhone.ServerValidate
    > If txt1.Text = "" And txt2.Text = "" Then
    > args.IsValid = False
    > Else
    > args.IsValid = True
    > End If
    > End Sub
    >
    > The server validation is fired only if there is some value in txt2 box.If
    > txt2.text is empty then server validation is not fired.
    > How can I solve this problem?
    >
    > I would like also to include client validation.
    >
    > Thank you,
    > Simon
    DujHoD, May 27, 2004
    #3
  4. simon

    simon Guest

    Hi,

    If I get rid of the ControlToValidate property then validation function is
    executed but when I set
    args.isValid=false, nothing happened.
    It just pass by like if I set args.isValid=true.
    No difference.
    Any idea?

    Thank you,
    Simon


    "DujHoD" <> wrote in message
    news:...
    > Try getting rid of the ControlToValidate property. It's not necessary
    > for a CustomValidator, and in this case, it may be what is preventing
    > the ServerValidate event from firing.
    >
    > For client-side validation, use the ClientValidationFunction property
    > of the CustomValidator.
    >
    >
    > "simon" <> wrote in message

    news:<#>...
    > > I have 2 <asp:textbox controls>:
    > >
    > > <asp:textbox id="txt1" Runat="server" ></asp:textbox>
    > >
    > > <asp:textbox id="txt2" Runat="server" ></asp:textbox>
    > >
    > > User must insert the value either in txt1 OR in txt2, so one of them
    > > shouldn't be empty.
    > >
    > > So I use custom validator:
    > >
    > > <asp:CustomValidator id="valPhone" Runat="server" ErrorMessage="You

    must
    > > insert text1 or text2!"
    > > ControlToValidate="txt2"
    > > Display="Dynamic"></asp:CustomValidator>
    > >
    > > AND server:
    > >
    > > Private Sub valPhone_ServerValidate(ByVal source As Object, ByVal args

    As
    > > System.Web.UI.WebControls.ServerValidateEventArgs) Handles
    > > valPhone.ServerValidate
    > > If txt1.Text = "" And txt2.Text = "" Then
    > > args.IsValid = False
    > > Else
    > > args.IsValid = True
    > > End If
    > > End Sub
    > >
    > > The server validation is fired only if there is some value in txt2

    box.If
    > > txt2.text is empty then server validation is not fired.
    > > How can I solve this problem?
    > >
    > > I would like also to include client validation.
    > >
    > > Thank you,
    > > Simon
    simon, May 28, 2004
    #4
  5. simon

    MATTXtwo

    Joined:
    Oct 8, 2008
    Messages:
    2
    lol...you must be on tension right now...
    check this line: ControlToValidate="txt2"
    so.....do you get it?
    It only to control validate for txt2
    MATTXtwo, Mar 25, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Mackay
    Replies:
    0
    Views:
    2,671
    Colin Mackay
    Jun 25, 2003
  2. Libs
    Replies:
    0
    Views:
    1,478
  3. Colin Basterfield

    Web form validation vs object validation

    Colin Basterfield, Nov 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    416
    Tommy
    Nov 29, 2003
  4. Matt
    Replies:
    14
    Views:
    4,061
    Chad Z. Hower aka Kudzu
    Jan 30, 2004
  5. =?Utf-8?B?QmFyYmFyYSBBbGRlcnRvbg==?=

    Using Summary Validation control with server Custom validation

    =?Utf-8?B?QmFyYmFyYSBBbGRlcnRvbg==?=, Oct 15, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    868
    =?Utf-8?B?QmFyYmFyYSBBbGRlcnRvbg==?=
    Oct 15, 2004
Loading...

Share This Page