Home All Groups Group Topic Archive Search About
Author
28 Nov 2006 3:59 PM
C
Hi,

I have a C# app where I want validate a year range (1900 - 2006) using a
Regular Expression.

Am not familiar with writing Reg Exp's at all.

Anyone got one that does something similar with Years?

Tnks.

Author
28 Nov 2006 5:56 PM
Rad [Visual C# MVP]
There are a couple of ways to do this:

Windows Application

1) Use a pair of spinner control and then the validation would be to
get the values of each and check that they are in the range. Main
benefit of this is your validation code is less, since you don't have
to use regexes at all. Personally I'd pick this approach

2) If you are using textboxes you must use a little more work.
a) Check that both text boxes contain numbers
b) Convert these numbers to integers
c) Check that they fall in the range

To check that the text is valid you'd do this in a function

Regex r = new Regex(@"(\d+)");
//
// Get the input
//
string input = "59";
//
// Capture the value and see if its valid
//
bool valid= !r.IsMatch(input);

Then you can do a plain vanilla integer comparison

Web Application
1) Again, minimize the effort. You will need 3 validators
a) A Required validator
b) A compare validator, set operator set to datatypecheck, type set to
integer
c) A range validator, with 1900 as the minumumvalue and 2006 as the
maximumvalue

On Tue, 28 Nov 2006 07:59:03 -0800, C <C@discussions.microsoft.com>
wrote:

>Hi,
>
>I have a C# app where I want validate a year range (1900 - 2006) using a
>Regular Expression.
>
>Am not familiar with writing Reg Exp's at all.
>
>Anyone got one that does something similar with Years?
>
>Tnks.
--

Bits.Bytes.
http://bytes.thinkersroom.com
Author
1 Feb 2007 7:56 PM
Göran Andersson
C wrote:
> Hi,
>
> I have a C# app where I want validate a year range (1900 - 2006) using a
> Regular Expression.
>
> Am not familiar with writing Reg Exp's at all.
>
> Anyone got one that does something similar with Years?
>
> Tnks.

Divide the range into ranges that can more easily be expressed as text
matches:

1900 - 1999
2000 - 2006

These can be matched with these expressions:

19\d\d
200[0-6]

Put them together in an expression:

(19\d\d)|(200[0-6])

--
Göran Andersson
_____
http://www.guffa.com

AddThis Social Bookmark Button