Validation of empty, or range of values in a text box

F

Fraggle

Hi,

I want to validate a text box, The user can leave it blank, or fill it
in. If they fill it in then it must be a date within a certain range.

How can I achieve this?

Thank you very much
 
R

Richard K Bethell

Fraggle said:
Hi,

I want to validate a text box, The user can leave it blank, or fill it
in. If they fill it in then it must be a date within a certain range.

How can I achieve this?

I'd do that as a server side validation... you don't even need the validator
for that. Throw a label in the area you want the error message to appear.
Write a boolean function that returns true if the text box is blank, or if
not, parses with DateTime.Parse to a date within your range. Then call the
function from your submit button's event

R.
 
F

Fraggle

Richard K Bethell said:
I'd do that as a server side validation... you don't even need the validator
for that. Throw a label in the area you want the error message to appear.
Write a boolean function that returns true if the text box is blank, or if
not, parses with DateTime.Parse to a date within your range. Then call the
function from your submit button's event

R.

Thanks.

Maybe one day I will actually do something simple enough that uses the
more "advanced" features of the .net framework, rather than having to
custom write EVERYTHING!
 
P

Peter Blum

Oh boy! That suggestion actually is much harder than using a validator.

Use the RangeValidator. It ignores blank fields automatically. It only
checks for errors when there is some text. Set Type=Date and your minimum
and maximum dates in the Minimum and Maximum properties.

Note that this will not report an error if the date entered is an illegal
format.
For that, use CompareValidator, Operator=DataTypeCheck, Type=Date.

Here are several ways to enhance your user interface:
1. You can combine the logic of several validators into one using the
MultiConditionValidator, part of my Professional Validation And More
product. (http://www.peterblum.com/vam/home.aspx) It includes 22 validators
and greatly expands what you can do with validators. You can learn about the
limitations of Microsofts validators here:
http://www.peterblum.com/vam/valmain.aspx to use as you design your site.

2. There are many really nice date textboxes with popup calendars available
from third parties. They are listed at the www.asp.net Control Gallery,
www.123aspx.com, and www.411asp.net. Mine is Peter's Date Package
(http://www.peterblum.com/datecontrols/home.aspx). It includes validators,
time entry, and stand-alone calendar that doesn't require post backs.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 
C

Chris Moore

I would have to agree that using a validator would be a much simpler way to go. This is exactly what they were designed for.

Chris
--------------------
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top