how to validate datetime value?

J

Jeff

hi

asp.net 3.5

In my webpage users can enter a date, it's the uses (Day of Birth). The
problem is that I don't know how to validate that date.

I use a RequiredFieldValidator to make sure user enters a value, but not
sure how to use the RegularExpressionValidator. I mean I don't know how
RegularExpressionValidator because AFAIK, RegularExpressionValidator has a
pattern it test the field against. But the format of dates can vary
depending on the culture where the user live... I mean some places uses
dates like dd.mm.yy and some other places uses dates like mm.dd.yy... so How
can I verify that a date is correct when the format of the date vary?
 
G

Gregory A. Beamer

I like Mark's answer. If this is not an option for you, you will need to
start looking into either dividing the date into separate boxes (labeled
month, day and year?) or using culture information when you load the
datetime structure.

You can use the Regex validator here, if you desire. You will just have to
set up a regex that can validate the different formats. With dates, there
are not too many. The Regex library might have what you desire:
http://regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=5

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Blog:
http://feeds.feedburner.com/GregoryBeamer

*********************************************
| Think outside the box |
*********************************************
 
A

Andy B.

Hi. If they don't want to use a regEx validator or use a calendar then there
is a very good, easy and lightweight answer to this problem. If you use C#
this solution should take only a few extra lines of code since my original
solution is targeted directly for VB features. Here we go...
This walkthrough covers my problem I had to solve directly, but you can draw
from it what you need.

1. Create a Visual Studio web/website application
2. On default.aspx, drop a wizard control.
3. In WizardStep1 drop a validationSummary, 2 textBoxes and 2
customValidator controls.
4. Set the properties of the customValidators and TextBoxes as you need.
Make sure the ControlToValidate in the customValidators point to the right
textBoxes.
5. For clarity sake, I named my customValidators DateValidator and
TimeValidator and my TextBoxes DateTextBox and TimeTextBox.
6. With step 1 showing in the designer (change wizard.ActiveStepIndex to 0),
double click the DateValidator and enter the following code in the event
handler. For the C# version, make sure you have using Microsoft.VisualBasic;
at the top of the code file and that you have added
Microsoft.VisualBasic.dll to the project references.
[C#]
if(Information.IsDate(Args.Value) == false) {
Args.IsValid = false;
}

7. Since the IsDate method is a native VB function and is contained inside a
module, all you have to do is use the IsDate method without any extra steps.
Put this code in the customValidator event:
[VB]
if(IsDate(Args.Value) = false) then
Args.IsValid = false
end if
8. In the wizard.NextButton_Click event (right click wizard control in the
designer, go to properties and click the lightening bolt button) double
click the nextButton_Click event and add this code.
]C#]
if(Page.IsValid == true) {
//Any code that you want the wizard to do when changing wizard steps if the
customValidators validate.
}
else {
e.Cancel = true;
}

[VB]
if(Page.IsValid = true) Then
'*** Whatever you want the wizard to do whenever you click next.
else
E.Cancel = true
end if

After this short bit of code (it looks like a lot, but only takes 5 minutes
to actually drop into a page) you should be able to run the page and click
the next button. Make sure you enter some valid and invalid dates and times.
This will validate/invalidate any form a date/time value can or can't take.
Trust me I pushed it to its limmits *grin*. Also make sure you put the
customValidator event handler code in both DateValidator and TimeValidator
event handlers. You can use this method of validating date/time values on a
single dateTime value as well. I just split the date and time in different
sections because it is what I had to do and it shows the availability of the
IsDate method. Have fun validating!
 
A

Andy B.

For the C# version, make sure you have using Microsoft.VisualBasic; at the
top of the code file and that you have added Microsoft.VisualBasic.dll to
the project references.

You can't possibly be serious...

What's wrong with using a VB dll in C#? It's like there is a huge problem
with VB or something. It's the fastest and easiest way to get a date
validated isnt it?
7. Since the IsDate method is a native VB function and is contained inside
a module, all you have to do is use the IsDate method without any extra
steps.

Dim blnIsDate1 As Boolean = IsDate("02/13/2008")
Dim blnIsDate2 As Boolean = IsDate("13/02/2008")

Both of the above dates are perfectly valid depending on where you live...



I know...
 

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

Top