Regular Expression

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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
 
C said:
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])
 
Back
Top