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
KavyaVSAsked:
Who is Participating?
 
Gary DavisConnect With a Mentor Dir 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
 
Gary DavisConnect With a Mentor Dir 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 DavisConnect With a Mentor Dir 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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
HainKurtConnect With a Mentor Sr. 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
 
Terry WoodsConnect With a Mentor IT 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 WoodsConnect With a Mentor IT 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 DavisConnect With a Mentor Dir 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
 
KavyaVSAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.