473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Custom Validator not working without RequiredValidator

Hi there,

I have written a custom validator function to validate a datefrom a user-filled field. The function tries to parse the dateand if it can't, sets (serverValidateEventArgs)e.IsValid tofalse.

The twist is that it allows blank dates to be entered - viz,e.IsValid==true if the field is left blank. The idea is to usethat validation control to make sure the date a user has enteredis valid, without the date field being required to complete theform.

The problem is that this doesn't work - although theCustomValidator itself does not show an error message when thefield is left blank, the ValidationSummary does show the errormessage, and Page.IsValid==false. The most troubling is that Iadded some logging code to the validation function which doesn'tseem to get called, suggesting that the validation functiondoesn't actully get called...
The validation function is as follows:

----------------

public static void ValidateShortDate(object sender,ServerValidateEventArgs e)
{
logger.Debug("Trying to validate a date");
if(StringUtil.IsEmpty(e.Value))
{
e.IsValid=true;
logger.Debug("Date to validate is empty, returningvalid=true");
return;
}

DateTime dt = DateTime.MinValue;
try
{
dt = DateTime.ParseExact(e.Value.Trim(), "d",CultureInfo.CurrentCulture);
}
catch (Exception ex)
{
e.IsValid = false;
logger.Debug("Error parsing date to validate, returningvalid=false", ex);
return;
}
if (dt.Equals(DateTime.MinValue))
{
e.IsValid = false;
logger.Debug("Date to validate==DateTime.MinValue, returningvalid=false");
return;
}
logger.Debug("Date to validate is valid ("+dt+"), returningvalid=true");
e.IsValid = true;
}
----------------
and in the aspx page:

<asp:TextBox runat="server" id="_dateOfBirthCalendar" />
<!--
<asp:RequiredFieldValidator ID="_dateOfBirthValidator1"ControlToValidate="_dat eOfBirthCalendar" Runat="server"
ErrorMessage="Please enter a valid birth date" />
-->
<asp:CustomValidator ID="_dateOfBirthValidator"ControlToValidate="_date OfBirthCalendar"
Runat="server" ErrorMessage="Please enter a valid birth date"/>

----------------
I replaced a custom control by the TextBox in this example.

Note that uncommenting the RequireValidator, the code performs asexpected (ie an error messgage appears both next to the fieldand in the ValidatorSummary)

Any help greatly appreciated.

--------------------------------
From: Elie Medeiros

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>07vGoINLaUymVyisY/SgAg==</Id>
Nov 19 '05 #1
1 2753
Try removing the ControlToValidate property from your CustomValidator. If
the ControlToValidate is left blank, the validator will fire all the time
without working with the RequiredFieldValidator.

You might want to add your own attribute that defines the ControlToValidate
so you know what control to check on the server.

HTH,

bill
"Elie Medeiros via .NET 247" <an*******@dotnet247.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi there,

I have written a custom validator function to validate a date from a
user-filled field. The function tries to parse the date and if it can't,
sets (serverValidateEventArgs)e.IsValid to false.

The twist is that it allows blank dates to be entered - viz, e.IsValid==true
if the field is left blank. The idea is to use that validation control to
make sure the date a user has entered is valid, without the date field being
required to complete the form.

The problem is that this doesn't work - although the CustomValidator itself
does not show an error message when the field is left blank, the
ValidationSummary does show the error message, and Page.IsValid==false. The
most troubling is that I added some logging code to the validation function
which doesn't seem to get called, suggesting that the validation function
doesn't actully get called...
The validation function is as follows:

----------------

public static void ValidateShortDate(object sender, ServerValidateEventArgs
e)
{
logger.Debug("Trying to validate a date");
if(StringUtil.IsEmpty(e.Value))
{
e.IsValid=true;
logger.Debug("Date to validate is empty, returning valid=true");
return;
}

DateTime dt = DateTime.MinValue;
try
{
dt = DateTime.ParseExact(e.Value.Trim(), "d",
CultureInfo.CurrentCulture);
}
catch (Exception ex)
{
e.IsValid = false;
logger.Debug("Error parsing date to validate, returning valid=false", ex);
return;
}
if (dt.Equals(DateTime.MinValue))
{
e.IsValid = false;
logger.Debug("Date to validate==DateTime.MinValue, returning valid=false");
return;
}
logger.Debug("Date to validate is valid ("+dt+"), returning valid=true");
e.IsValid = true;
}
----------------
and in the aspx page:

<asp:TextBox runat="server" id="_dateOfBirthCalendar" />
<!--
<asp:RequiredFieldValidator ID="_dateOfBirthValidator1"
ControlToValidate="_dateOfBirthCalendar" Runat="server"
ErrorMessage="Please enter a valid birth date" />
-->
<asp:CustomValidator ID="_dateOfBirthValidator"
ControlToValidate="_dateOfBirthCalendar"
Runat="server" ErrorMessage="Please enter a valid birth date" />

----------------
I replaced a custom control by the TextBox in this example.

Note that uncommenting the RequireValidator, the code performs as expected
(ie an error messgage appears both next to the field and in the
ValidatorSummary)

Any help greatly appreciated.

--------------------------------
From: Elie Medeiros

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>07vGoINLaUymVyisY/SgAg==</Id>
Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Microsoft | last post by:
Hello there, I have following situation , how can I solve this puzzle in C#. I have interface called IVendor with some custom attributes applied to it interface IVendor {
2
by: Jim Heavey | last post by:
Hello, I created a simple javascipt routine to do some validation. I attached that script to my ASP Button with the following code... cmdAddTask.Attributes = "javascript:return...
5
by: tshad | last post by:
I was looking at a Custom Validator that 4 Guys had posted that would solve my RequiredValidator problem with CheckBoxLists and it says it can't find the .dll. But the dll is in both the Bin...
2
by: Alan Silver | last post by:
Hello, I have a custom validator on my page, and have the server-side code working fine. I want to add a client-side funtion as well, but am not sure how to wire it in so that it works with the...
9
by: wardy1975 | last post by:
Hi All, Looking for a little expert advice on a few web standards issues. I am currently trying to understand the impact of web standards for a web application I work with. I have been doing a...
0
by: tsw_mik | last post by:
I have created a custom control. It has: -label -button -list of textboxes -list of dropdownlists. I want to use a custom validator to perform some validation, but somehow I can't fo it. The...
4
by: Rick | last post by:
Hello, I built a composite web control that has a textbox and a date control. added my custom control on a webform where there are other standard controls. Each control on the form has a...
0
by: mayankagarwal | last post by:
Over the last few days i have read a lot of forum about custom validator not working or not firing etc. I am stuck in a similar situation and of all the solutions i have come across they dont seem to...
1
gagandeepgupta16
by: gagandeepgupta16 | last post by:
Hi I am working on an entry form using validation controls in ASP.NET. I have two controls which requires custom validators, no issue in using plain custom validators. But when i am using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.