Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 9th, 2004, 01:57 AM
Authorized User
 
Join Date: Aug 2004
Location: , , .
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
Default validation involving 2 controls

How can I validate 2 textboxes using customvalidator? The textboxes are mutually exclusive. Only one of them should be filled. I cannot find any validator which can check the value entered in both the boxes and then send the message back via validationsummary. I can validate only one control at a time.


 
Old December 9th, 2004, 05:32 AM
Friend of Wrox
 
Join Date: Jun 2004
Location: Chennai, Tamil Nadu, India.
Posts: 449
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to r_ganesh76
Default

You can set the IsValid Property of the custom validator to false in your application.

Regards
Ganesh
 
Old December 9th, 2004, 05:33 AM
Friend of Wrox
 
Join Date: Jun 2004
Location: Chennai, Tamil Nadu, India.
Posts: 449
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to r_ganesh76
Default

I am not sure if that is the best practice. I have used it in that way.

Regards
Ganesh
 
Old December 9th, 2004, 07:24 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

you can use IsValid Property of your page,every validator is checked if all validators are true it returns true,othrewise all values in ErrorMessage property of validators are collected in ValidationSummary and will be displayed,
Take a look at this example,
Code:
<Script Runat="Server">

Sub Button_Click( s As Object, e As EventArgs )
  If IsValid Then
    Response.Redirect( "ThankYou.aspx" )
  End If
End Sub

</Script>

<html>
<head><title>ValidationSummary.aspx</title></head>
<body>

<form Runat="Server">

<asp:ValidationSummary
  HeaderText="There are problems with the following
    form fields:"
  Runat="Server" />

<p>

First Name:
<br>
<asp:TextBox
  ID="txtFirstname"
  Runat="Server" />

<asp:RequiredFieldValidator
  ID="reqVal1"
  ControlToValidate="txtFirstname"
  Text="You must enter a first name!"
  ErrorMessage="First Name"
  Runat="Server" />

<p>

Last Name:
<br>
<asp:TextBox
  ID="txtLastname"
  Runat="Server" />

<asp:RequiredFieldValidator
  ControlToValidate="txtLastname"
  Text="You must enter a last name!"
  ErrorMessage="Last Name"
  Runat="Server" />

<p>

Occupation:
<br>
<asp:TextBox
  ID="txtOccupation"
  Runat="Server" />

<asp:RequiredFieldValidator
  Text="You must enter an occupation!"
  ControlToValidate="txtOccupation"
  ErrorMessage="Occupation"
  Runat="Server" />

<p>

<asp:Button
  Text="Submit"
  OnClick="Button_Click"
  Runat="Server"/>

</form>

</body>
</html>
HtH.

_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.
 
Old December 9th, 2004, 11:32 PM
Authorized User
 
Join Date: Aug 2004
Location: , , .
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My customvalidator's OnServerValidation does not seem to fire. TextValidate1 is the OnServerValidation function. I even tried calling customvalidator's validate method in page_load also. I want the message to be part of the validation summary. Right now I am only moving the value to variable called "msg" to debug. The Isvalid property is always true, even if I don't enter anything in the textbox1. Here is my code

 Protected Sub TextValidate1(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
        args.IsValid = (args.Value <> "")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CustomValidator1.Validate()
        Dim msg As String
        If IsValid Then
            msg = "good"
        Else
            msg = "bad"
        End If
    End Sub

The HTML is :-

<form id="Form1" runat="server">
            <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 104px; POSITION: absolute; TOP: 16px" runat="server"></asp:TextBox>
            <asp:TextBox id="TextBox2" style="Z-INDEX: 102; LEFT: 104px; POSITION: absolute; TOP: 48px" runat="server"></asp:TextBox>
            <asp:CustomValidator id="CustomValidator1" style="Z-INDEX: 103; LEFT: 272px; POSITION: absolute; TOP: 16px"
                runat="server" ErrorMessage="txtbox1 must be entered" ControlToValidate="TextBox1" OnServerValidate="TextValidate1"
                Display="Dynamic"></asp:CustomValidator>
            <asp:CustomValidator id="CustomValidator2" style="Z-INDEX: 104; LEFT: 272px; POSITION: absolute; TOP: 48px"
                runat="server" ErrorMessage="txtbox2 must be entered" ControlToValidate="TextBox2" Display="Dynamic"></asp:CustomValidator>
            <asp:Button id="Button1" style="Z-INDEX: 105; LEFT: 176px; POSITION: absolute; TOP: 88px" runat="server"
                Text="Button"></asp:Button>
        </form>


 
Old December 10th, 2004, 04:34 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

here you have two TextBoxes,and you want to validate them in Server-Side,if I guessed your meaning correctly,I think you need two functions for two custom validators for example one of your function could be like below,
Code:
Protected Sub TextValidate1(ByVal source As System.Object, ByVal args As 
    System.Web.UI.WebControls.ServerValidateEventArgs)
       If (e.Value=="Renu")
        args.IsValid = True
       Else
        args.IsValid = False
       End If 
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim msg As String
        If IsValid Then
            msg = "good"
        Else
            msg = "bad"
        End If
End Sub
//dont change Page_Load
//and your HTML
<asp:CustomValidator id="CustomValidator1" style="Z-INDEX: 103; LEFT: 272px; POSITION: absolute; TOP: 16px" runat="server" ErrorMessage="txtbox1 must be entered" ControlToValidate="TextBox1" OnServerValidate="TextValidate1" Display="Dynamic"></asp:CustomValidator>

then for custom validator2,do it like above(make another function,do validation
operations on TextBox2 through args.Value in CustomValidator2_ServerValidate event ....


_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.
 
Old December 10th, 2004, 11:32 AM
Authorized User
 
Join Date: Aug 2004
Location: , , .
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Mehdi-
I realize that I need 2 different validation controls and 2 different functions to validate, but I was trying to use customvalidator to see how it works. I have specified textvalidate1 as the Onservervalidation function, why doesn't it seem to fire. When I debug the button_click function, the isvalid property always remains true. If I can make this work then I can compare the isvalid value from both the controls and display the error message accordingly. Also, Can I display the error message in the validation summary along with other error messages or I will have to create a lable and display the output?

 
Old December 10th, 2004, 12:15 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

well,CustomValidators dont work for empty fields,I mean if your TextBox remains empty
CustomValidator returns true(in such situations in addittion to CustumValidators,you can
use RequiredFiledValidators and set its EnableClienScript to false)
then you can simply use ValidationSummary(all values in ErrorMessage properties of-
validators are collected in ValidationSummary and will be displayed),
HtH.

_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.
 
Old December 10th, 2004, 12:50 PM
Authorized User
 
Join Date: Aug 2004
Location: , , .
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok, if customvalidator cannot check empty fields, let's forget it. I used 2 RequiredFieldValidators with EnableClientScript to False and in the button_click event checking the isvalid property for both. If they both are same, that is a case when I need to show error. If both are empty or both are filled, that is an error situation. I cannot understand how I can use both customvalidator and requiredfield validator to check this. Pelase advise. I am attaching the code and html portion. You can quickly copy it and try it. Your help will be greatly appreciated.
Thanks a lot for looking into it.

Code:-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If RequiredFieldValidator1.IsValid = RequiredFieldValidator2.IsValid Then

            ' Then I need to show the error in validation summarybox. How do I do that?

        End If
    End Sub


HTML:-
<form id="Form1" runat="server">
            <asp:textbox id="TextBox1" style="Z-INDEX: 101; LEFT: 80px; POSITION: absolute; TOP: 8px" runat="server"
                Width="76px"></asp:textbox><asp:textbox id="TextBox2" style="Z-INDEX: 102; LEFT: 80px; POSITION: absolute; TOP: 40px" runat="server"
                Width="76px"></asp:textbox><asp:customvalidator id="CustomValidator1" style="Z-INDEX: 103; LEFT: 208px; POSITION: absolute; TOP: 8px"
                runat="server" Display="Dynamic" ControlToValidate="TextBox1" ErrorMessage="txtbox1 must be entered" Enabled="False"></asp:customvalidator><asp:customvalidator id="CustomValidator2" style="Z-INDEX: 104; LEFT: 208px; POSITION: absolute; TOP: 32px"
                runat="server" Display="Dynamic" ControlToValidate="TextBox2" ErrorMessage="txtbox2 must be entered" Enabled="False"></asp:customvalidator><asp:button id="Button1" style="Z-INDEX: 105; LEFT: 96px; POSITION: absolute; TOP: 88px" runat="server"
                Text="Button"></asp:button>
            <asp:RequiredFieldValidator id="RequiredFieldValidator1" style="Z-INDEX: 106; LEFT: 168px; POSITION: absolute; TOP: 8px"
                runat="server" Display="Dynamic" ErrorMessage="Either box1 or box 2 must be entered, NOT Both!" EnableClientScript="False"
                ControlToValidate="TextBox1">*</asp:RequiredFieldValidator>
            <asp:ValidationSummary id="ValidationSummary1" style="Z-INDEX: 107; LEFT: 64px; POSITION: absolute; TOP: 144px"
                runat="server" ShowMessageBox="True"></asp:ValidationSummary>
            <asp:RequiredFieldValidator id="RequiredFieldValidator2" style="Z-INDEX: 108; LEFT: 168px; POSITION: absolute; TOP: 40px"
                runat="server" Display="Dynamic" ControlToValidate="TextBox2" ErrorMessage="Either box1 or box2 must be entered, not both!"
                EnableClientScript="False">*</asp:RequiredFieldValidator></form>

 
Old December 11th, 2004, 07:56 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

I didnt get your condition,(I think your condition was somewhat strange so
it's better not to work with Page.IsValid)...
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
 Handles Button1.Click
        Dim msg As String
        'check every IsValid property
        If (RequiredFieldValidator1.IsValid or
              RequiredFieldValidator1.IsValid or OtherValidators.IsValid)               Then 'Optional 
            msg = "good"
        Else
            msg = "bad"
        End If
End Sub
make some several Validators(including RequiredFieldValidator and CompareValidator) and
change your If-Statement in Button_Click event according to your condition...

_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation controls and postback sdi126 ASP.NET 2.0 Basics 5 November 11th, 2007 12:08 PM
User controls and validation relaytest49 ASP.NET 2.0 Professional 1 October 11th, 2006 01:25 PM
Validation Controls andyj00 ASP.NET 1.0 and 1.1 Professional 3 June 9th, 2005 09:19 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.