Validating Dates

T

tshad

I have 4 dropdown boxes (fromDay, fromMonth, ToDay and ToMonth)

I want to be able to test to make sure that the dates are valid and the the
From dates are < the To dates.

When I add the records to the database, I typically use something like:

FromMonth.SelectedValue & "/01/" & FromYear.SelectedValue

I also have other validators on the screen for emails and required fields.

How would I set up a validator to handle this situation?

I was looking at setting a Custom validator or a CompareValidator to handle
the fields. I would need to move the variables into a temporary variable
for testing (sting or label).

The problem is that it would need to fire after I set up the temporary
variables. All the other validators would fire on submit.

How would I set up this validator to fire after the others and after I have
set up the variables for it to Validate?

Thanks,

Tom
 
C

Curt_C [MVP]

tshad said:
I have 4 dropdown boxes (fromDay, fromMonth, ToDay and ToMonth)

I want to be able to test to make sure that the dates are valid and the the
From dates are < the To dates.

When I add the records to the database, I typically use something like:

FromMonth.SelectedValue & "/01/" & FromYear.SelectedValue

I also have other validators on the screen for emails and required fields.

How would I set up a validator to handle this situation?

I was looking at setting a Custom validator or a CompareValidator to handle
the fields. I would need to move the variables into a temporary variable
for testing (sting or label).

The problem is that it would need to fire after I set up the temporary
variables. All the other validators would fire on submit.

How would I set up this validator to fire after the others and after I have
set up the variables for it to Validate?

Thanks,

Tom
Mixing validators can be a real pain. This is the reason I ended up
moving to 100% CustomValidators. You still dont get much control on the
order they validate, but at least they will all validate at the same
time. If you need to control the order you may want to create your own
labels/etc and roll it all into one function that fires on IsPostBack()
 
T

tshad

Curt_C said:
Mixing validators can be a real pain. This is the reason I ended up moving
to 100% CustomValidators. You still dont get much control on the order
they validate, but at least they will all validate at the same time. If
you need to control the order you may want to create your own labels/etc
and roll it all into one function that fires on IsPostBack()

But how do I get the CustomValidator to fire after I have set up the
asp:Labels:

FromDate.Text = FromMonth.SelectedValue & "/01/" & FromYear.SelectedValue
ToDate.Text = ToMonth.SelectedValue & "/01/" & ToYear.SelectedValue

Validate it now

Also, what would be the best way to validate it? I am trying to make sure
the FromDate is before or equal to the ToDate

Thanks,

Tom
 
C

Curt_C [MVP]

But how do I get the CustomValidator to fire after I have set up the
asp:Labels:


The custom validator should fire on the postback automatically, if not
do a page.Validate() call and a page.IsValid() check to see if it passed.
 
T

tom pester

Hi Tom,

I see this question pop up on the newsgroup from time to time so I gave it
a go.
The code below works but sometimes needs 2 button clicks to fire a valid
date combination to the server.
I'll track that issue if I have time or let me know if you solved it.

Some things I found out while coding :
- Validators can't reference hiddenfields for validation. I had to use textboxes
with a display:none to let the validators do its work
- The CampareValidator in Date mode doest support the time component (1/1/2000
is ok but 1/1/2000 17:18 is not)

The below code uses my local for dates which is d/m/y.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
RegisterOnSubmitStatement("keyXYZ", "Combine();");
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>

<script language="javascript">

function Combine(){
document.getElementById("TextBox5").value = '1/' + document.getElementById("TextBox1").value
+ '/' + document.getElementById("TextBox2").value;
document.getElementById("TextBox6").value = '1/' + document.getElementById("TextBox3").value
+ '/' + document.getElementById("TextBox4").value;
}
</script>

<form id="form1" runat="server">
<div>
Month<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>Year<asp:TextBox
ID="TextBox2"
runat="server"></asp:TextBox>&nbsp;<asp:TextBox ID="TextBox5"
Style="display: none"
runat="server" ></asp:TextBox>
<br />
Month<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>Year
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox6" Style="display: none" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="TextBox6"
ControlToValidate="TextBox5" ErrorMessage="CompareValidator"
Operator="LessThan"
Type="Date"></asp:CompareValidator></div>
</form>
</body>
</html>


Let me know if it helped you or not...

Cheers,
Tom Peste
 
L

Lucas Tam

I have 4 dropdown boxes (fromDay, fromMonth, ToDay and ToMonth)

I want to be able to test to make sure that the dates are valid and
the the From dates are < the To dates.

You can use IsDate to test if the Date is valid.

Once you know it is a date, you can cast the string to a date and do a > or
< comparison.
 
T

tshad

Curt_C said:
The custom validator should fire on the postback automatically, if not do
a page.Validate() call and a page.IsValid() check to see if it passed.

But I don't want it to fire on PostBack automatically because I want to set
up the Date Labels first.

Would I have to set the Button CausesValidation to false first, Set the
Labels and then do Valiate()?

Tom
 
T

tshad

tom pester said:
Hi Tom,

I see this question pop up on the newsgroup from time to time so I gave it
a go.
The code below works but sometimes needs 2 button clicks to fire a valid
date combination to the server.
I'll track that issue if I have time or let me know if you solved it.

It doesn't help if I have to do 2 button clicks to make it work.

Are you doing it in Javascript to get it done before the Validation?

Tom
 
T

tom pester

Hi Tom,

Its a client side javascript soltion ( and it will also validate on the server
afterwards ).
Check out RegisterOnSubmitStatement. Can you see whats going on?

Cheers,
Tom Pester
 
C

Curt_C [MVP]

tshad said:
The custom validator should fire on the postback automatically, if not do
a page.Validate() call and a page.IsValid() check to see if it passed.


But I don't want it to fire on PostBack automatically because I want to set
up the Date Labels first.

Would I have to set the Button CausesValidation to false first, Set the
Labels and then do Valiate()?

Tom
Why would you need to set the labels FIRST? just fill em in the
postback. They are already clicking the button. Fill em with clientsdie
code otherwise.
 
T

tshad

Curt_C said:
tshad said:
But I don't want it to fire on PostBack automatically because I want to set
up the Date Labels first.

Would I have to set the Button CausesValidation to false first, Set the
Labels and then do Valiate()?

Tom

Why would you need to set the labels FIRST? just fill em in the
postback. They are already clicking the button. Fill em with clientsdie
code otherwise.

Does it handle the IsPostback code before it Validates()?

If that is true, you're right - I can do it there.

Tom
 
M

michaelcoleman72

Use a custom validator(s). You can make one for each date element (mm,
dd, yy) and/or one for the entire date. Set the control to validate
for the entire date on the the last item. The indivduals will fire as
you change each control. So if you select/add 31 in the dd field and
the mm is set to two, then the dd field will fire. The validators fire
in the order they are added to the page. (Have not validated this but
that is how it appears to work.) The final control validates the
entire date. You will need to reference all of the date controls in
your JavaScript. I have found it easier to add an array or custom
variables in script during postback to then reference in my validation
script functions. That way you have the unique id of the control as it
is rendered. Good luck and good coding.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top