I'm using a CompareValidator and a CustomValidator in one Form, which causes me trouble with the ValidationSummary:
Case I
CompareValidated Control is valid, CustomValidated control is invalid ==> ValidationSummary works (shows the CustomValidated Errormessage)
Case II
CompareValidated Control is invalid, CustomValidated control is valid ==> ValidationSummary works (shows the CompareValidated Errormessage)
Case III
CompareValidated Control is invalid, CustomValidated control is invalid ==>
ValidationSummary works incorrect (shows only the CompareValidated Errormessage)
This is my Code
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="CustomValid_Master.aspx.vb" Inherits="Japlas_Kaizen.CustomValid_Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<!--Invalid if < 100-->
Compare:
<asp:TextBox id="txtCompare"
runat="server" />
<asp:CompareValidator ID="CompareValidator1"
ControlToValidate="txtCompare"
Display="Dynamic"
Operator="GreaterThanEqual"
ValueToCompare="100"
Text="*"
ErrorMessage="Comparison not valid (Value < 100)!"
runat="server"></asp:CompareValidator>
<br />
<!--Invalid if not odd value-->
Custom:
<asp:TextBox id="txtCustom"
runat="server" />
<asp:CustomValidator id="CustomValidator1"
ControlToValidate="txtCustom"
Display="Dynamic"
ErrorMessage="Custoam not valid (Not an even number)!"
runat="server"
Text="*" />
<br />
<!--Validation Summary displays only CompareValidator ErrorMessage if both (CompareValidator, CustomValidator) are invalid-->
<asp:ValidationSummary ID="ValidationSummary" runat="server"
CssClass="ValidationSummary" DisplayMode="List"
HeaderText="Bitte überprüfen Sie folgende Eingabe/-n:" />
<br />
<!--Submit Button-->
<asp:Button id="Button1"
Text="Validate"
runat="server"/>
</asp:Content>
CodeBehind:
Sub ServerValidation(ByVal source As Object, ByVal arguments As ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
Dim num As Integer = Integer.Parse(arguments.Value)
arguments.IsValid = ((num Mod 2) = 0)
End Sub
I just figured out, if I set the EnableClientScript on both (CustomValidator, CompareValidator) to false it works! But now it's not dynamic anymore, the page is beeing validate after the button click event but not after the textfield changed event anymore.
Any ideas why it works with
EnableClientScript="False"
and how I can get it t work with validation on each textbox changed event?
I just figured out, if I set the EnableClientScript on both (CustomValidator, CompareValidator) to false it works! But now it's not dynamic anymore, the page is beeing validate after the button click event but not after the textfield changed event anymore.
Any ideas why it works with
EnableClientScript="False"
The validation controls work on both client side (javascript) and Server side (.net code) by default. Client sde validation is performed first, only for the validation controls that support both client and server side validation. So if you implemented a
custom control without client validation, the validation of the custom control will only be performed when the validationcontrols that suppirt client validation are valid. If one or more client validators are invalid, serverside validation wil not occur and
the validation summarry will only show the errors thrown by the client validators
So when you disable clientside validation, all validatio will occur simultanious on the server.
ronin47
and how I can get it t work with validation on each textbox changed event?
The TextBox Changed event occurs on server side. So it needs a postback. When you set the
AutoPostBack Property of the TextBox to true, a postback will occor when the TextBox control loses focus. To perform the validation, you need to set the
CausesValidation Property of the TextBox to True, because this is False by default (for a Button control, the default value is True!)
ronin47
Unfortunately it's not possible for me to use js since I actually going to run a db query to (custom)validate the control on my other page...
You can use javascript/Jquery to call a webservice where you perform your valudation
The TextBox Changed event occurs on server side. So it needs a postback. When you set the
AutoPostBack Property of the TextBox to true, a postback will occor when the TextBox control loses focus. To perform the validation, you need to set the
CausesValidation Property of the TextBox to True, because this is False by default (for a Button control, the default value is True!)
Thx for this great answer. I modified my code and got this
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="CustomValid_Master.aspx.vb" Inherits="JK.CustomValid_Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<ajaxtoolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</ajaxtoolkit:ToolkitScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<!--Invalid if < 100-->
Compare:
<asp:TextBox id="txtCompare"
runat="server" AutoPostBack="True" CausesValidation="True" />
<asp:CompareValidator ID="CompareValidator1"
ControlToValidate="txtCompare"
Display="Dynamic"
Operator="GreaterThanEqual"
ValueToCompare="100"
Text="*"
ErrorMessage="Comparison not valid (Value < 100)!"
runat="server" EnableClientScript="False" ></asp:CompareValidator>
<br />
<!--Invalid if not odd value-->
Custom:
<asp:TextBox id="txtCustom"
runat="server" AutoPostBack="True" CausesValidation="True" />
<asp:CustomValidator id="CustomValidator1"
ControlToValidate="txtCustom"
Display="Dynamic"
ErrorMessage="Custoam not valid (Not an even number)!"
runat="server"
Text="*" EnableClientScript="False" />
<br />
<!--Validation Summary displays only CompareValidator ErrorMessage (CompareValidator, CustomValidator) if both are invalid-->
<asp:ValidationSummary ID="ValidationSummary" runat="server"
CssClass="ValidationSummary" DisplayMode="List"
HeaderText="Bitte überprüfen Sie folgende Eingabe/-n:" />
<br />
<!--Submit Button-->
<asp:Button id="Button1"
Text="Validate"
runat="server"/>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Since I only use server side validation now it works pretty fine, except the following constellation (capitalized letters stand for int values):
I) I type an invalid value 'A' into txtCompare --> ValidationSummary displays Error on txtCompare
II) I type an invalid value 'B' into txtCustom --> ValidationSummary displays Error on txtCompare, txtCustom
III) I type a valid value 'X' into txtCompare --> ValidationSummary displays Error on txtCustom (txtCompare is valid now)
IV) I type a valid value 'Y' into txtCustom --> ValidationSummary displays no error (txtCustom is valid now)
V) I type an invalid value 'C' into txtCompare --> ValidationSummary displays no error (actually txtCompare is invalid now!!)
VI) I type an invalid value 'A' (same as in I) into txtCompare --> ValidationSummary displays Error on txtCompare
When I now type any invalid value beside 'A' into txtCompare it will stay valid! Why? Is it a bug or is it my mistake?
In the example above I used the following values:
A = 1 (invalid on txtCompare)
B = 1 (invalid on txtCustom)
C = 2 (invalid on txtCompare but no error was displayed)
X = 100 (valid on txtCompare)
Y = 12 (valid on txtCustom)
Thx for any help, hope my information helps to solve the problem
ronin47
Member
62 Points
73 Posts
CompareValidator and CustomValidator in one Form
Apr 27, 2012 12:51 PM|LINK
Hey Guys
I'm using a CompareValidator and a CustomValidator in one Form, which causes me trouble with the ValidationSummary:
Case I
CompareValidated Control is valid, CustomValidated control is invalid ==> ValidationSummary works (shows the CustomValidated Errormessage)
Case II
CompareValidated Control is invalid, CustomValidated control is valid ==> ValidationSummary works (shows the CompareValidated Errormessage)
Case III
CompareValidated Control is invalid, CustomValidated control is invalid ==> ValidationSummary works incorrect (shows only the CompareValidated Errormessage)
This is my Code
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="CustomValid_Master.aspx.vb" Inherits="Japlas_Kaizen.CustomValid_Master" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <!--Invalid if < 100--> Compare: <asp:TextBox id="txtCompare" runat="server" /> <asp:CompareValidator ID="CompareValidator1" ControlToValidate="txtCompare" Display="Dynamic" Operator="GreaterThanEqual" ValueToCompare="100" Text="*" ErrorMessage="Comparison not valid (Value < 100)!" runat="server"></asp:CompareValidator> <br /> <!--Invalid if not odd value--> Custom: <asp:TextBox id="txtCustom" runat="server" /> <asp:CustomValidator id="CustomValidator1" ControlToValidate="txtCustom" Display="Dynamic" ErrorMessage="Custoam not valid (Not an even number)!" runat="server" Text="*" /> <br /> <!--Validation Summary displays only CompareValidator ErrorMessage if both (CompareValidator, CustomValidator) are invalid--> <asp:ValidationSummary ID="ValidationSummary" runat="server" CssClass="ValidationSummary" DisplayMode="List" HeaderText="Bitte überprüfen Sie folgende Eingabe/-n:" /> <br /> <!--Submit Button--> <asp:Button id="Button1" Text="Validate" runat="server"/> </asp:Content>CodeBehind:
Sub ServerValidation(ByVal source As Object, ByVal arguments As ServerValidateEventArgs) Handles CustomValidator1.ServerValidate Dim num As Integer = Integer.Parse(arguments.Value) arguments.IsValid = ((num Mod 2) = 0) End SubAny ideas what I'm doing wrong?
tusharrs
Contributor
3230 Points
668 Posts
Re: CompareValidator and CustomValidator in one Form
Apr 27, 2012 12:56 PM|LINK
hi,
you have to use a javascript method for custom validation on aspx page not the servervalidate event of custom validator
example
<SCRIPT LANGUAGE="JavaScript"> function validatefunction(oSrc, args){ //logic for odd number
} </SCRIPT> <asp:Textbox id="text1" runat="server" text=""> </asp:Textbox> <asp:CustomValidator id="CustomValidator1" runat=server ControlToValidate = "text1" ErrorMessage = "You must enter at least 8 characters!" ClientValidationFunction="validatefunction" > </asp:CustomValidator>
( Mark as Answer if it helps you out )
View my Blog
ronin47
Member
62 Points
73 Posts
Re: CompareValidator and CustomValidator in one Form
Apr 27, 2012 01:00 PM|LINK
Really? Is there no other way? Because it works if I type the CompareValidated Control is valid.
Unfortunately it's not possible for me to use js since I actually going to run a db query to (custom)validate the control on my other page...
ronin47
Member
62 Points
73 Posts
Re: CompareValidator and CustomValidator in one Form
Apr 27, 2012 01:17 PM|LINK
Notice this case! It works using the CodeBehind without calling any JavaScript function!
ronin47
Member
62 Points
73 Posts
Re: CompareValidator and CustomValidator in one Form
Apr 27, 2012 02:22 PM|LINK
I just figured out, if I set the EnableClientScript on both (CustomValidator, CompareValidator) to false it works! But now it's not dynamic anymore, the page is beeing validate after the button click event but not after the textfield changed event anymore.
Any ideas why it works with
and how I can get it t work with validation on each textbox changed event?
thx
tusharrs
Contributor
3230 Points
668 Posts
Re: CompareValidator and CustomValidator in one Form
Apr 28, 2012 09:17 AM|LINK
hi,
you can use javascript function to validate
<script language="JavaScript" type="text/javascript">
function validatefunction() {
if (document.getElementById('<%= txtCompare.ClientID %>').value != null) {
if (eval(document.getElementById('<%= txtCompare.ClientID %>').value) < 100) {
alert('Compare value must be greater than or equal to 100');
return;
}
}
if (document.getElementById('<%= txtCustom.ClientID %>').value != null) {
if ((eval(document.getElementById('<%= txtCustom.ClientID %>').value) % 2) != 0) {
alert('Custom - Not an even number');
}
}
}
</script>
Compare:
<asp:TextBox id="txtCompare"
runat="server" />
<br />
<!--Invalid if not odd value-->
Custom:
<asp:TextBox id="txtCustom"
runat="server" />
<br />
<asp:ValidationSummary ID="ValidationSummary" runat="server"
CssClass="ValidationSummary" DisplayMode="List"
HeaderText="Bitte überprüfen Sie folgende Eingabe/-n:" />
<br />
<!--Submit Button-->
<asp:Button id="Button1" Text="Validate" runat="server" OnClientClick="validatefunction()"/>
( Mark as Answer if it helps you out )
View my Blog
hans_v
All-Star
35986 Points
6550 Posts
Re: CompareValidator and CustomValidator in one Form
Apr 28, 2012 10:07 AM|LINK
The validation controls work on both client side (javascript) and Server side (.net code) by default. Client sde validation is performed first, only for the validation controls that support both client and server side validation. So if you implemented a custom control without client validation, the validation of the custom control will only be performed when the validationcontrols that suppirt client validation are valid. If one or more client validators are invalid, serverside validation wil not occur and the validation summarry will only show the errors thrown by the client validators
So when you disable clientside validation, all validatio will occur simultanious on the server.
The TextBox Changed event occurs on server side. So it needs a postback. When you set the AutoPostBack Property of the TextBox to true, a postback will occor when the TextBox control loses focus. To perform the validation, you need to set the CausesValidation Property of the TextBox to True, because this is False by default (for a Button control, the default value is True!)
You can use javascript/Jquery to call a webservice where you perform your valudation
http://www.2ndbrain.com/devToolbox/showArticle.aspx?aID=87
http://www.mikesdotnetting.com/Article/99/Preventing-duplicate-User-Names-with-ASP.NET-and-jQuery
An easier way, you can also turn of client side validation (as you already found out), and to prevent a full postback, use an AJAX UpdatePanel
http://msdn.microsoft.com/en-us/library/bb399001.aspx
ramiramilu
All-Star
95503 Points
14106 Posts
Re: CompareValidator and CustomValidator in one Form
Apr 28, 2012 10:11 AM|LINK
its working fine in C# - all your code working good...
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { } protected void Validate(object source, ServerValidateEventArgs args) { int num = Int32.Parse(args.Value); args.IsValid = ((num % 2) == 0); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <!--Invalid if < 100--> Compare: <asp:TextBox id="txtCompare" runat="server" /> <asp:CompareValidator ID="CompareValidator1" ControlToValidate="txtCompare" Display="Dynamic" Operator="GreaterThanEqual" ValueToCompare="100" Text="*" ErrorMessage="Comparison not valid (Value < 100)!" runat="server"></asp:CompareValidator> <br /> <!--Validation Summary displays only CompareValidator ErrorMessage if both (CompareValidator, CustomValidator) are invalid--> <asp:ValidationSummary ID="ValidationSummary" runat="server" CssClass="ValidationSummary" DisplayMode="List" HeaderText="Bitte überprüfen Sie folgende Eingabe/-n:" /> <br /> <!--Invalid if not odd value--> Custom: <asp:TextBox id="txtCustom" runat="server" /> <asp:CustomValidator id="CustomValidator1" ControlToValidate="txtCustom" OnServerValidate="Validate" Display="Dynamic" ErrorMessage="Custoam not valid (Not an even number)!" runat="server" Text="*" /> <br /> <!--Submit Button--> <asp:Button id="Button1" Text="Validate" runat="server"/> </div> </form> </body> </html>JumpStart
ronin47
Member
62 Points
73 Posts
Re: CompareValidator and CustomValidator in one Form
Apr 29, 2012 01:15 PM|LINK
Hey tusharrs,
Thx for your answer but it won't work like that because in my real code I perform some database requests to validate the controls.
ronin47
Member
62 Points
73 Posts
Re: CompareValidator and CustomValidator in one Form
Apr 29, 2012 01:38 PM|LINK
Thx for this great answer. I modified my code and got this
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="CustomValid_Master.aspx.vb" Inherits="JK.CustomValid_Master" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <ajaxtoolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> </ajaxtoolkit:ToolkitScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <!--Invalid if < 100--> Compare: <asp:TextBox id="txtCompare" runat="server" AutoPostBack="True" CausesValidation="True" /> <asp:CompareValidator ID="CompareValidator1" ControlToValidate="txtCompare" Display="Dynamic" Operator="GreaterThanEqual" ValueToCompare="100" Text="*" ErrorMessage="Comparison not valid (Value < 100)!" runat="server" EnableClientScript="False" ></asp:CompareValidator> <br /> <!--Invalid if not odd value--> Custom: <asp:TextBox id="txtCustom" runat="server" AutoPostBack="True" CausesValidation="True" /> <asp:CustomValidator id="CustomValidator1" ControlToValidate="txtCustom" Display="Dynamic" ErrorMessage="Custoam not valid (Not an even number)!" runat="server" Text="*" EnableClientScript="False" /> <br /> <!--Validation Summary displays only CompareValidator ErrorMessage (CompareValidator, CustomValidator) if both are invalid--> <asp:ValidationSummary ID="ValidationSummary" runat="server" CssClass="ValidationSummary" DisplayMode="List" HeaderText="Bitte überprüfen Sie folgende Eingabe/-n:" /> <br /> <!--Submit Button--> <asp:Button id="Button1" Text="Validate" runat="server"/> </ContentTemplate> </asp:UpdatePanel> </asp:Content>Since I only use server side validation now it works pretty fine, except the following constellation (capitalized letters stand for int values):
I) I type an invalid value 'A' into txtCompare --> ValidationSummary displays Error on txtCompare
II) I type an invalid value 'B' into txtCustom --> ValidationSummary displays Error on txtCompare, txtCustom
III) I type a valid value 'X' into txtCompare --> ValidationSummary displays Error on txtCustom (txtCompare is valid now)
IV) I type a valid value 'Y' into txtCustom --> ValidationSummary displays no error (txtCustom is valid now)
V) I type an invalid value 'C' into txtCompare --> ValidationSummary displays no error (actually txtCompare is invalid now!!)
VI) I type an invalid value 'A' (same as in I) into txtCompare --> ValidationSummary displays Error on txtCompare
When I now type any invalid value beside 'A' into txtCompare it will stay valid! Why? Is it a bug or is it my mistake?
In the example above I used the following values:
A = 1 (invalid on txtCompare)
B = 1 (invalid on txtCustom)
C = 2 (invalid on txtCompare but no error was displayed)
X = 100 (valid on txtCompare)
Y = 12 (valid on txtCustom)
Thx for any help, hope my information helps to solve the problem