ASP.NET Forms CustomCustomvalidator - always correct value - asp.net

I've created simple page in ASP.NET Forms and I need to use CustomValidator control. Here's the code:
Page.aspx
<asp:ListBox
ID="listControl" runat="server"
ClientIDMode="Static"
onchange="toggleButton(this, document.getElementById('remove'))" />
<asp:CustomValidator
ID="fmValidator"
ControlToValidate="listControl"
CssClass="IncorrectResult"
runat="server"
ValidateEmptyText="True"
ErrorMessage="..."
Text="..."
OnServerValidate="Validator_OnServerValidate" />
Page.aspx.cs
protected void Validator_OnServerValidate(object source, ServerValidateEventArgs args)
{
if(isAdmin)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
The point is that I can catch an event in Validator_OnServerValidate with proper data but whatever I put in args.IsValid (I mean true or false) it always returns correct value - on the webpage everything is correct. What is wrong?

That was because page didn't reload on validating. All I had to do was enable client side validation.

Related

ASP.Net Custom Validator failed ,form gets submitted

I have made a form in which there are two RAD DateTimePicker Controls . One is for Start-DateTime and other is for End Date Time. Inside Custom Validator, I have Compared the Date Time Picked So far and hence made it valid or invalid accordingly its Server Validate event code goes like this.
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) {
if (rdpEndDate.SelectedDate < rdpStartDate.SelectedDate) {
args.IsValid = false;
} else {
args.IsValid = true;
}
}
Its Design Code goes like this.
<telerik:RadDateTimePicker ID="rdpStartDate" runat="server" AutoPostBackControl="Both" onselecteddatechanged="rdpStartDate_SelectedDateChanged">
<TimeView CellSpacing="-1" Culture="en-IN">
</TimeView>
<TimePopupButton HoverImageUrl="" ImageUrl="" />
<Calendar UseColumnHeadersAsSelectors="False" UseRowHeadersAsSelectors="False" ViewSelectorText="x">
</Calendar>
<DateInput AutoPostBack="True" DateFormat="dd-MM-yyyy" DisplayDateFormat="dd-MM-yyyy">
</DateInput>
<DatePopupButton HoverImageUrl="" ImageUrl="" />
</telerik:RadDateTimePicker>
<asp:Label ID="Label2" runat="server" Text=" To" CssClass="h_text"></asp:Label>
<telerik:RadDateTimePicker ID="rdpEndDate" runat="server" onselecteddatechanged="rdpEndDate_SelectedDateChanged" AutoPostBackControl="Both">
<TimeView CellSpacing="-1" Culture="en-IN"></TimeView>
<TimePopupButton ImageUrl="" HoverImageUrl=""></TimePopupButton>
<Calendar UseRowHeadersAsSelectors="False" UseColumnHeadersAsSelectors="False" ViewSelectorText="x"></Calendar>
<DateInput DisplayDateFormat="dd-MM-yyyy" DateFormat="dd-MM-yyyy" AutoPostBack="True"></DateInput>
<DatePopupButton ImageUrl="" HoverImageUrl=""></DatePopupButton>
</telerik:RadDateTimePicker>
Validator Source Code in designer is like this.
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="rdpEndDate"
ErrorMessage="End Date Cant be Before Start Date"
OnServerValidate="CustomValidator1_ServerValidate" SetFocusOnError="True"
ValidateEmptyText="True" ValidationGroup="submit">End Date Cant be Before Start Date</asp:CustomValidator>
I want to ask that even when custom validator fails, My form gets submitted with faulty values. What can be the reason? How can I avoid that?
With Server validator Event like:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) {
if (rdpEndDate.SelectedDate < rdpStartDate.SelectedDate) {
args.IsValid = false;
} else {
args.IsValid = true;
}
}
You have to check on your server event as well like:(For example if you are using your validator with button click then)
protected void btn_OnClick(object sender, EventArgs e)
{
if (Page.IsValid)
{
Response.Write("Page is valid.");
}
else
{
Response.Write("Page is not valid!");
}
}
My suggestion: Telerik has a good client side support as well so I suggest you to use client side validation of custom validator.
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="rdpEndDate"
ErrorMessage="End Date Cant be Before Start Date"
ClientValidationFunction="CheckDates"
SetFocusOnError="True"
ValidateEmptyText="True" ValidationGroup="submit">End Date Cant be Before Start Date</asp:CustomValidator>
then in Javascript:
function CheckDates(sender, args)
{
var cltRdpEndDate= $find("<%=rdpEndDate.ClientID %>");
var cltRdpStartDate= $find("<%=rdpStartDate.ClientID %>");
if(cltRdpEndDate.get_dateInput().get_selectedDate()< cltRdpStartDate.get_dateInput().get_selectedDate())//if your condtion fails here
{
args.IsValid = false;
return;
}
args.IsValid = true;
}

DotNetNuke CustomValidator strange behavior

I have a strange problem with a dotnetnuke module I'm developing.
I want to use a asp custom validator to validate some input. To keep it simple I'll check only if the field was not empty and at least a few characters long. (I know there are other standard validators that I can use).
The problem is that my code works ok locally (development), but not on production.
The only difference I know is that I use DNN 6 instead of DNN 5.
No matter what I type in on production site, it always shows me the validation error message.
These are the relevant parts of the webpage:
ASCX:
<div>
<asp:UpdatePanel ID="UpdatePanelValidationSummaryHome" ChildrenAsTriggers="False" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:ValidationSummary ID="ValidationSummary1" CssClass="validationSummary" runat="server"
EnableClientScript="False" ShowSummary="true" ShowMessageBox="false" />
<asp:CustomValidator ID="CustomValidatorActiveTab" runat="server" Display="None"
ErrorMessage="Error the field ... was not correct..." OnServerValidate="CustomValidatorActiveTab_ServerValidate"></asp:CustomValidator>
<asp:Button ID="btnZoeken" CssClass="btnZoeken" CausesValidation="true" runat="server" Text="<%$ Resources:GLOBAL, btnZoeken %>"
OnClick="btnZoeken_Click" />
Code behind
private bool ValidateTab_Ondernemingsnummer()
{
if (!String.IsNullOrEmpty(txtOndernemingsnummer.Text) && txtOndernemingsnummer.Text.Length >= 3)
{
return true;
}
return false;
}
protected void CustomValidatorActiveTab_ServerValidate(object source, ServerValidateEventArgs e)
{
int activeTab = GetActiveIndexAccordion();
switch (activeTab)
{
//Zoeken op ondernemingsnummer
case 0:
if (!ValidateTab_Ondernemingsnummer())
{
e.IsValid = false;
}
else
{
e.IsValid = true;
}
break;
}
Thanks for any help.
Any ideas?
SOLVED:
I used dotnetnuke logging to see when and why e.isValid is set to false.
My custom validator control was being called twice!!
The first time it was validated ok, the second time it was not.
My solution was to disable custom server validator control in the markup and enable it just after doing the submit (and don't forget to turn it off).
Like this:
<asp:UpdatePanel ID="UpdatePanelValidationSummaryHome" ChildrenAsTriggers="False"
UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:ValidationSummary ID="ValidationSummary1" CssClass="validationSummary" runat="server"
EnableClientScript="False" ShowSummary="true" ShowMessageBox="false" />
<asp:CustomValidator ID="CustomValidatorActiveTab" runat="server" Display="None"
EnableClientScript="false" Enabled="false" ErrorMessage="ERROR ONDERNEMINGSNUMMER"
OnServerValidate="CustomValidatorActiveTab_ServerValidate"></asp:CustomValidator>
Enabled = false is important here!
Then in the button click
protected void btnZoeken_Click(object sender, EventArgs e)
{
CustomValidatorActiveTab.Enabled = true;
CustomValidatorActiveTab.Validate();
if (Page.IsValid)
{
CustomValidatorActiveTab.Enabled = false;
I still don't know why the CustomValidatorActiveTab_ServerValidate was being called twice.
It has something to do with DNN 5 I suppose (and maybe it was fixed in DNN 6).
I hope this helped someone.
The reason the validator is called twice is due to the link button.
Generating the GridView onBubbleEvent, which also causes the validator to validate.

CustomValidator not being called

I have a CustomValidator that is being used to check username availability, but it isn't getting called (I also checked it with a breakpoint).
<asp:CustomValidator ID="usernameC" runat="server" ErrorMessage="Username is already taken" ControlToValidate="txtUsername" Display="None" OnServerValidate="usernameC_ServerValidate" />
Code-behind
protected void usernameC_ServerValidate(object source, ServerValidateEventArgs args)
{
if (new UsersBL().SearchUserByUsername(args.Value) == null)
args.IsValid = true;
else
args.IsValid = false;
}
What may be the problem?
it will be called when the page is being posted to the server. e.g. you have an asp:button and when the user click it the page is sent to the server and before it calls the button_click event it calls the server validation function.

How do I make a checkbox required on an ASP.NET form?

I've done some searching on this, and I've found several partial answers, however nothing that gives me that warm fuzzy "this is the right way to do this". To answer the most frequently occurring complaint against this question: "checkboxes can have two legitimate states - checked and unchecked", this is an "I accept the terms and conditions..." checkbox which must be checked in order to complete a registration, hence checking the box is required from a business logic standpoint.
Please provide complete cut-n-paste ready code fragments with your response! I know there are several pieces to this -- the CustomValidator (presumably), the code-behind, some javascript and possibly a check for IsValid, and the frustrating part for me is that in each example I've seen, one of these critical pieces is missing!
javascript function for client side validation (using jQuery)...
function CheckBoxRequired_ClientValidate(sender, e)
{
e.IsValid = jQuery(".AcceptedAgreement input:checkbox").is(':checked');
}
code-behind for server side validation...
protected void CheckBoxRequired_ServerValidate(object sender, ServerValidateEventArgs e)
{
e.IsValid = MyCheckBox.Checked;
}
ASP.Net code for the checkbox & validator...
<asp:CheckBox runat="server" ID="MyCheckBox" CssClass="AcceptedAgreement" />
<asp:CustomValidator runat="server" ID="CheckBoxRequired" EnableClientScript="true"
OnServerValidate="CheckBoxRequired_ServerValidate"
ClientValidationFunction="CheckBoxRequired_ClientValidate">You must select this box to proceed.</asp:CustomValidator>
and finally, in your postback - whether from a button or whatever...
if (Page.IsValid)
{
// your code here...
}
C# version of andrew's answer:
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="Please accept the terms..."
onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
<asp:CheckBox ID="CheckBox1" runat="server" />
Code-behind:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = CheckBox1.Checked;
}
If you want a true validator that does not rely on jquery and handles server side validation as well ( and you should. server side validation is the most important part) then here is a control
public class RequiredCheckBoxValidator : System.Web.UI.WebControls.BaseValidator
{
private System.Web.UI.WebControls.CheckBox _ctrlToValidate = null;
protected System.Web.UI.WebControls.CheckBox CheckBoxToValidate
{
get
{
if (_ctrlToValidate == null)
_ctrlToValidate = FindControl(this.ControlToValidate) as System.Web.UI.WebControls.CheckBox;
return _ctrlToValidate;
}
}
protected override bool ControlPropertiesValid()
{
if (this.ControlToValidate.Length == 0)
throw new System.Web.HttpException(string.Format("The ControlToValidate property of '{0}' is required.", this.ID));
if (this.CheckBoxToValidate == null)
throw new System.Web.HttpException(string.Format("This control can only validate CheckBox."));
return true;
}
protected override bool EvaluateIsValid()
{
return CheckBoxToValidate.Checked;
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (this.Visible && this.Enabled)
{
System.Web.UI.ClientScriptManager cs = this.Page.ClientScript;
if (this.DetermineRenderUplevel() && this.EnableClientScript)
{
cs.RegisterExpandoAttribute(this.ClientID, "evaluationfunction", "cb_verify", false);
}
if (!this.Page.ClientScript.IsClientScriptBlockRegistered(this.GetType().FullName))
{
cs.RegisterClientScriptBlock(this.GetType(), this.GetType().FullName, GetClientSideScript());
}
}
}
private string GetClientSideScript()
{
return #"<script language=""javascript"">function cb_verify(sender) {var cntrl = document.getElementById(sender.controltovalidate);return cntrl.checked;}</script>";
}
}
Scott's answer will work for classes of checkboxes. If you want individual checkboxes, you have to be a little sneakier. If you're just doing one box, it's better to do it with IDs. This example does it by specific check boxes and doesn't require jQuery. It's also a nice little example of how you can get those pesky control IDs into your Javascript.
The .ascx:
<script type="text/javascript">
function checkAgreement(source, args)
{
var elem = document.getElementById('<%= chkAgree.ClientID %>');
if (elem.checked)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
function checkAge(source, args)
{
var elem = document.getElementById('<%= chkAge.ClientID %>');
if (elem.checked)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
</script>
<asp:CheckBox ID="chkAgree" runat="server" />
<asp:Label AssociatedControlID="chkAgree" runat="server">I agree to the</asp:Label>
<asp:HyperLink ID="lnkTerms" runat="server">Terms & Conditions</asp:HyperLink>
<asp:Label AssociatedControlID="chkAgree" runat="server">.</asp:Label>
<br />
<asp:CustomValidator ID="chkAgreeValidator" runat="server" Display="Dynamic"
ClientValidationFunction="checkAgreement">
You must agree to the terms and conditions.
</asp:CustomValidator>
<asp:CheckBox ID="chkAge" runat="server" />
<asp:Label AssociatedControlID="chkAge" runat="server">I certify that I am at least 18 years of age.</asp:Label>
<asp:CustomValidator ID="chkAgeValidator" runat="server" Display="Dynamic"
ClientValidationFunction="checkAge">
You must be 18 years or older to continue.
</asp:CustomValidator>
And the codebehind:
Protected Sub chkAgreeValidator_ServerValidate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ServerValidateEventArgs) _
Handles chkAgreeValidator.ServerValidate
e.IsValid = chkAgree.Checked
End Sub
Protected Sub chkAgeValidator_ServerValidate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ServerValidateEventArgs) _
Handles chkAgeValidator.ServerValidate
e.IsValid = chkAge.Checked
End Sub
I typically perform the validation on the client side:
<asp:checkbox id="chkTerms" text=" I agree to the terms" ValidationGroup="vg" runat="Server" />
<asp:CustomValidator id="vTerms"
ClientValidationFunction="validateTerms"
ErrorMessage="<br/>Terms and Conditions are required."
ForeColor="Red"
Display="Static"
EnableClientScript="true"
ValidationGroup="vg"
runat="server"/>
<asp:Button ID="btnSubmit" OnClick="btnSubmit_Click" CausesValidation="true" Text="Submit" ValidationGroup="vg" runat="server" />
<script>
function validateTerms(source, arguments) {
var $c = $('#<%= chkTerms.ClientID %>');
if($c.prop("checked")){
arguments.IsValid = true;
} else {
arguments.IsValid = false;
}
}
</script>
Non-javascript way . .
aspx page:
<form id="form1" runat="server">
<div>
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:CustomValidator ID="CustomValidator1"
runat="server" ErrorMessage="CustomValidator" ControlToValidate="CheckBox1"></asp:CustomValidator>
</div>
</form>
Code Behind:
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
If Not CheckBox1.Checked Then
args.IsValid = False
End If
End Sub
For any actions you might need (business Rules):
If Page.IsValid Then
'do logic
End If
Sorry for the VB code . . . you can convert it to C# if that is your pleasure. The company I am working for right now requires VB :(

How do I validate that a list box is not empty (client side)

I'm working with ASP.NET 3.5.
I have a list box that users must add items to (I've written the code for this). My requirement is that at least one item must be added to the listbox or they cannot submit the form. I have several other validators on the page and they all write to a ValidationSummary control. I would like this listbox validation to write to the Validation Summary control as well. Any help is greatly appreciated. Thank you.
Drop in a custom validator, Add your desired error message to it, double click on the custom validator to get to the code behind for the event handler, and then you would implement server side like this:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = ListBox1.Items.Count > 0;
}
Also you can implement client side javascript as well.
I just threw this up on a page and tested it quickly, so you might need to tweak it a bit: (The button1 only adds an item to the List Box)
<script language="JavaScript">
<!--
function ListBoxValid(sender, args)
{
args.IsValid = sender.options.length > 0;
}
// -->
</script>
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" ValidationGroup="NOVALID" />
<asp:Button ID="Button2" runat="server" Text="ButtonsUBMIT" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="CustomValidator"
onservervalidate="CustomValidator1_ServerValidate" ClientValidationFunction="ListBoxValid"></asp:CustomValidator>
If you add a validation summary to the page, you error text should show up in that summary if there is no items in the ListBox, or other collection-able control, what ever you want to use, as long as the ValidationGroup is the same.
This didn't work for me:
function ListBoxValid(sender, args)
{
args.IsValid = sender.options.length > 0;
}
But this did:
function ListBoxValid(sender, args)
{
var ctlDropDown = document.getElementById(sender.controltovalidate);
args.IsValid = ctlDropDown.options.length > 0;
}
gotta make sure to add these properties to the CustomValidator:
Display="Dynamic" ValidateEmptyText="True"
<asp:CustomValidator
runat="server"
ControlToValidate="listbox1"
ErrorMessage="Add some items yo!"
ClientValidationFunction="checkListBox"
/>
<script type="Text/JavaScript">
function checkListBox(sender, args)
{
args.IsValid = sender.options.length > 0;
}
</script>
Actually this is the proper way to make this work (as far as the JavaScript is concerned).
ListBox.options.length will always be your total number of options, not the number you have selected. The only way I have found that works is to use a for loop to go through the list.
function ListBoxValid(sender, args)
{
var listBox = document.getElementById(sender.controltovalidate);
var listBoxCnt = 0;
for (var x =0; x<listBox.options.length; x++)
{
if (listBox.options[x].selected) listBoxCnt++;
}
args.IsValid = (listBoxCnt>0)
}
this work for me
<script language="JavaScript">
function CheckListBox(sender, args)
{
args.IsValid = document.getElementById("<%=ListBox1.ClientID%>").options.length > 0;
}
</script>
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="*Required" ClientValidationFunction="CheckListBox"></asp:CustomValidator>
You will want to register your control with the page by sending in the ClientID. Then, you can use Microsoft AJAX to grab your control and check the values.

Resources