• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 510
  • Last Modified:

Validating Year

Hi,
I am using VS2010 and C#.Net
1.I am trying to validate the years entered in a TextBox.
It can be two formats.Ex:2013-2012(fourdigitshyphenfourdigits) or 2009(fourdigits)

The first digit should be Two and second digit is zero and third digit is zero or one and fourth digit can be any thing 0-9

Thanks
0
KavyaVS
Asked:
KavyaVS
  • 7
  • 6
  • 4
  • +1
7 Solutions
 
Gary DavisDir Internet SvcsCommented:
Here is a regular expression. The 1st parameter is your input and the 2nd is the pattern:

if (Regex.IsMatch("2013-2012", @"20[01]\d-20[01]\d"))
{
   ...
}

Open in new window

Gary Davis
0
 
Gary DavisDir Internet SvcsCommented:
Ahh - to support both formats, make the hyphen and 2nd year optional:

Regex.IsMatch("2012", @"20[01]\d(-20[01]\d)?")

Open in new window

0
 
HainKurtSr. System AnalystCommented:
or something like this (without using regex) (not tested)

string s = TextBox.Text;
string s1 = s.Left(4);
string s2;
if s.Length()==9 s2=s.Rigt(4);

bool isNumber1;
int.TryParse(isNumber1, s1);
bool isValid1 = isNumber1 && int.Parse(s1)>=2000 && int.Parse(s1)<=2019;

bool isNumber2;
int.TryParse(isNumber2, s2);
bool isValid2 = (s2=="") || (isNumber2 && int.Parse(s2)>=2000 && int.Parse(s2)<=2019);

bool isAllValid = isValid1 && isValid2;

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Terry WoodsIT GuruCommented:
@gardavis, don't you need ^ and $ on that to prevent it matching values like "22012G"?

Regex.IsMatch("2012", @"^20[01]\d(-20[01]\d)?$")

Open in new window


Pleased to be of assistance in creating a year 2020 bug! ;-)
0
 
Gary DavisDir Internet SvcsCommented:
That would be correct if the field is not embedded within the input string.
0
 
KavyaVSAuthor Commented:
I need assistance for 2020 bug.

I used this expression for "^20[01]\\d(-20[01]\\d)?$"
It year should be 4 digits or it should be 4digitshyphen4digits.

The first digit should be Two and second digit should be zero and third digit can be zero or one or two and fourth digit can be any thing 0-9

Please let me know the changes.

Thanks
0
 
Terry WoodsIT GuruCommented:
This should do it:

Regex.IsMatch("2012", @"^20[012]\\d(-20[012]\\d)?$")

Open in new window

0
 
Terry WoodsIT GuruCommented:
Actually, from memory the backslashes don't need doubling when the @ is used before the string, I think:
Regex.IsMatch("2012", @"^20[012]\d(-20[012]\d)?$")

Open in new window

0
 
KavyaVSAuthor Commented:
Why we need the @ before the string.I am just validating the string entered in TextBox.
I tried it without @
"^20[012]\\d(-20[012]\\d)?$"


Thanks.
0
 
Gary DavisDir Internet SvcsCommented:
If you use the @, you don't double up the back-slashes. If you omit the @, you need to double up the back-slashes like you did.

Gary
0
 
KavyaVSAuthor Commented:
I tested 2011-2013 with "^20[012]\\d(-20[012]\\d)?$"

It is returning false.It should be true.
Why it is returning false please let me know.

Thanks.
0
 
Terry WoodsIT GuruCommented:
Do you have an @ character before the string? If so, then you need to only use single backslashes:
Regex.IsMatch("2011-2013", @"^20[012]\d(-20[012]\d)?$")

Open in new window

0
 
KavyaVSAuthor Commented:
I don't have the @ character before the string?
Please let me know what might causing the problem.

Thanks.
0
 
Gary DavisDir Internet SvcsCommented:
The regular expression you show above should match. Make sure your input string "2011-2013" has no other characters before or after.

Also, my 2nd answer (way above) is also correct.

Gary
0
 
KavyaVSAuthor Commented:
Empty spaces before and after the Hyphen causing the problems.How to take care of them.Ex:2013 -  2012.

The regular expression  can accept empty spaces before and after hyphen.

Thanks
0
 
Gary DavisDir Internet SvcsCommented:
Try this. I added space asterisk before and after the hyphen which means 0 or more spaces

if (Regex.IsMatch("2013 *- *2012", @"20[01]\d-20[01]\d"))
{
   ...
} 

Open in new window


If you know it is always one space before and after, you can remove the two asterisks.

Gary
0
 
Gary DavisDir Internet SvcsCommented:
I put it in the wrong place, sorry. Try this:

if (Regex.IsMatch("2013 - 2012", @"20[01]\d *- *20[01]\d"))
{
   ...
}  

Open in new window

0
 
KavyaVSAuthor Commented:
Thanks
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 7
  • 6
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now