cc1:DateValidator
ID="DateValidator1"
runat="server"
ControlToValidate="txtgeboortedatum"
ErrorMessage="Date is not a correct format"
Width="184px"
SetFocusOnError="True"></cc1:DateValidator>
in register.aspx.cs
DateTime
dt = DateTime.Parse(txtgeboortedatum.Text);
dt.ToShortDateString();
when i enter a incorrect value like "fffff" , the validator doesn't do his job and it fails in my code
The string was not recognized as a valid DateTime. There is a unknown word starting at index 0
Before you Parse your user's BirthDate, are you ensuring Page.IsValid. The Validator controls don't suddenly freeze execute of your code. You still need to make sure the page is valid. There are some situations where some postbacks do not need validation
called so it isn't "automatic" behavior.
If you set a break point inside your EvaluateIsValid method do it get hit?
Anytime you pass an illegal string format into a Parse() method, expect it to throw an exception describing a formatting problem. You should anticipate this by either catching the exception or validating it prior to this point.
If I was to guess, you were expecting the validator to validate and stop the code in Register.aspx.cs from running. If that's the case, you didn't put the required line
if (Page.IsValid)
{
// code to save or use the data.
}
In addition, ASP.NET comes with a validator to validate dates. Its the CompareValidator, with type=Date and Operator=DataTypeCheck. That article discusses this near the end.
--- Peter Blum
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
Kzz
Member
15 Points
3 Posts
problems with custom Date Validator
Jan 08, 2006 10:26 AM|LINK
i just writed a custom validator who can validate a textbox on a date , so the input is only a date
but it doesn't work [:S] ,what is my problem
Datevalidator.cs
using System;
using
System.Collections.Generic;using System.ComponentModel;
using
System.Text;using
System.Web;using
System.Web.UI;using
System.Web.UI.WebControls;namespace
ruilWCC{
[
DefaultProperty("Text")][
ToolboxData("<{0}:DateValidator runat=server></{0}:DateValidator>")] public class DateValidator:System.Web.UI.WebControls.BaseValidator{
TextBox txtb = new TextBox();[
Category("Appearance")][
DefaultValue("")][
Localizable(true)] protected override bool ControlPropertiesValid(){
Control ctrl = new Control();ctrl = FindControl(ControlToValidate);
if (!(ctrl.Equals(null))) {txtb =(
TextBox)ctrl; return true;}
else { return false;}
}
protected override bool EvaluateIsValid(){
Boolean lBolIsDateValid=true; String lStrInputDate = txtb.Text.ToString(); try{ DateTime.Parse(lStrInputDate);}
catch(Exception e){lBolIsDateValid =
false;};
return lBolIsDateValid;}
}
}
then i compile the dll en add a reference from my website library to the dll
in my masterpage :
<%
@ Register Assembly="ruilWCC" Namespace="ruilWCC" TagPrefix="DateValidator" %>in register.aspx
<
cc1:DateValidator ID="DateValidator1" runat="server" ControlToValidate="txtgeboortedatum" ErrorMessage="Date is not a correct format" Width="184px" SetFocusOnError="True"></cc1:DateValidator>in register.aspx.cs
DateTime
dt = DateTime.Parse(txtgeboortedatum.Text);dt.ToShortDateString();
when i enter a incorrect value like "fffff" , the validator doesn't do his job and it fails in my code
The string was not recognized as a valid DateTime. There is a unknown word starting at index 0what is the problem?
greetz Klaas
billrob458
Contributor
3329 Points
671 Posts
Re: problems with custom Date Validator
Jan 09, 2006 05:03 PM|LINK
If you set a break point inside your EvaluateIsValid method do it get hit?
protected void SaveUser_Click( .... )
{
if ( Page.IsValid )
{
DateTime dt = DateTime.Parse( Birthdate.Text );
}
}
bill
PLBlum
All-Star
30399 Points
5347 Posts
MVP
Re: problems with custom Date Validator
Jan 09, 2006 05:05 PM|LINK
Anytime you pass an illegal string format into a Parse() method, expect it to throw an exception describing a formatting problem. You should anticipate this by either catching the exception or validating it prior to this point.
If I was to guess, you were expecting the validator to validate and stop the code in Register.aspx.cs from running. If that's the case, you didn't put the required line
if (Page.IsValid)
{
// code to save or use the data.
}
For more on validators, see http://aspalliance.com/699.
In addition, ASP.NET comes with a validator to validate dates. Its the CompareValidator, with type=Date and Operator=DataTypeCheck. That article discusses this near the end.
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com