Solved

# Validating Year

Posted on 2012-08-28
498 Views
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
Question by:KavyaVS

LVL 18

Assisted Solution

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"))
{
...
}
``````
Gary Davis
0

LVL 18

Assisted Solution

Ahh - to support both formats, make the hyphen and 2nd year optional:

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

LVL 51

Assisted Solution

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;
``````
0

LVL 34

Assisted Solution

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

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

Pleased to be of assistance in creating a year 2020 bug! ;-)
0

LVL 18

Expert Comment

That would be correct if the field is not embedded within the input string.
0

Author Comment

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

LVL 34

Assisted Solution

This should do it:

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

LVL 34

Expert Comment

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)?\$")
``````
0

Author Comment

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

LVL 18

Expert Comment

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

Author Comment

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

LVL 34

Expert Comment

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)?\$")
``````
0

Author Comment

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

Thanks.
0

LVL 18

Expert Comment

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

Author Comment

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

LVL 18

Assisted Solution

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"))
{
...
}
``````

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

Gary
0

LVL 18

Accepted Solution

I put it in the wrong place, sorry. Try this:

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

Author Closing Comment

Thanks
0

## Featured Post

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the faâ€¦
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will wâ€¦
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: Tâ€¦