Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex for 2007-02-12 1

Status
Not open for further replies.

dal2

Technical User
May 16, 2005
34
US
Hi,
I'm not sure why it's not validating. Any help is greatly appreciated.
<td bgcolor="EEEEEE" align="right">
Production Date</td>
<td align="left" style="width: 811px">
<asp:TextBox size="40" Width="800" ID="txtProductionDate" AutoPostBack="False" runat="server" OnTextChanged="txtProductionDate_TextChanged" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server"
ErrorMessage="*"
ControlToValidate="txtProductionDate"
ValidationExpression="^[0-9-]((d\{4}) - (d\{2}) - (d\{2}))$" /></td>
 
Thanks anyway...I figured out...
"^\d{4}\-\d{2}\-\d{2}$"
The c# is on the server side validation....
 
That expression will validate all "dates" in the range:

0000-00-00 to 9999-99-99

Are you sure that that is what you want?

[tt](19|2[0-9])\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[/tt]

gives you the range 1900-01-01 to 2999-12-31, with 29th February always being valid. Although possible to check for that with a RegEx, it is very fiddly and would be much easier to do in your program.


Hope this helps.

[vampire][bat]
 
Correction:

I meant to say:

gives you valid dates in the range 1900-01-01 to 2999-12-31, with 29th February always being valid. Although possible to check for that with a RegEx, it is very fiddly and would be much easier to do in your program

[vampire][bat]
 
Actually, that RegEx gives you the 29th, 30th and 31st of each month, so still some room for improvement, although a simple switch statement would be able to confirm whether or not a returned "valid" date was actually valid.


HYope this helps.

[vampire][bat]
 
Thanks for your help. Please, explain the regex.
I ended up using datetime year, month, day and
validating on the server side.
 
(19|2[0-9])\d\d
19 OR 2 followed by any one number between 0 and 9 followed by two numbers

-
a dash

(0[1-9]|1[012])
0 follwed by any one number between 1 and 9 OR 1 followed by any one of 0, 1 or 2

-
a dash

(0[1-9]|[12][0-9]|3[01])
0 followed by any one number between 1 and 9 OR either 1 or 2 followed by any one number between 1 and 9 OR 3 followed by a 0 or a 1


Hope this helps.

[vampire][bat]
 
You could put the regex in this program into your validator, just remove the option and whitespace. earthandfire explained how they work so you should be able to read it.
Code:
using System;
using System.Text;
using System.Text.RegularExpressions;

namespace RegexTesting
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("1799-12-31 {0}", ValidateDate("1799-12-31"));//false no earlier than 1800-1-1
            Console.WriteLine("1800-1-1 {0}", ValidateDate("1800-1-1"));    //true
            Console.WriteLine("2007-2-7 {0}", ValidateDate("2007-2-7"));    //true
            Console.WriteLine("2007-02-07 {0}", ValidateDate("2007-02-07"));//true 
            Console.WriteLine("2007-4-31 {0}", ValidateDate("2007-4-31"));  //false April has 30 days
            Console.WriteLine("2007-2-29 {0}", ValidateDate("2007-2-29"));  //false
            Console.WriteLine("2008-2-29 {0}", ValidateDate("2008-2-29"));  //true leap year
            Console.WriteLine("9999-12-31 {0}", ValidateDate("9999-12-31"));//true 
        }

        public static bool ValidateDate(string yyyymmdd)
        {
            Regex dateRegex = new Regex(@"((^((1[8-9]\d{2})|([2-9]\d{3}))([-])(10|12|0?[13578])([-])(3[01]|[12][0-9]|0?[1-9])$)|
(^((1[8-9]\d{2})|([2-9]\d{3}))([-])(11|0?[469])([-])(30|[12][0-9]|0?[1-9])$)|
(^((1[8-9]\d{2})|([2-9]\d{3}))([-])(0?2)([-])(2[0-8]|1[0-9]|0?[1-9])$)|
(^([2468][048]00)([-])(0?2)([-])(29)$)| # this line down is for leap years, notice the 29
(^([3579][26]00)([-])(0?2)([-])(29)$)|
(^([1][89][0][48])([-])(0?2)([-])(29)$)|
(^([2-9][0-9][0][48])([-])(0?2)([-])(29)$)|
(^([1][89][2468][048])([-])(0?2)([-])(29)$)|
(^([2-9][0-9][2468][048])([-])(0?2)([-])(29)$)|
(^([1][89][13579][26])([-])(0?2)([-])(29)$)|
(^([2-9][0-9][13579][26])([-])(0?2)([-])(29)$))"
,RegexOptions.IgnorePatternWhitespace);
            return dateRegex.IsMatch(yyyymmdd);
        }
    }
}

Marty
 
dal2: you can always write yourself a windows application that's soul purpose is to test regexs. Just a textbox for the input, a textbox for the regex, and a go button.
 
Wow, thank you both for all this info!!!
Do you happen to know the formula for leap year finding?
Something like %4 %100 %400
Thanks again,
David
ps I'm working on video podcasting at uwtv.org
check out the cs files...
 
dal2,

If a year divides by 4 then it is a leap year unless it
also divides by 100 in which case it will only be a leap year if it divides by 400.

Therefore:

1800 No
1804, 8, 12 etc. Yes
1900 No
1904, 6 12 etc. Yes
2000 Yes
2100, 2200, 2300 No
2400 Yes

Hope this helps.

===============================

Marty, as I said fiddly - but well done. I started trying something along those lines, but I'm afraid I decided it was too much effort. A star methinks



[vampire][bat]
 
Thanks, for the explanation! I had an interview at Microsoft and they asked me to write a program on the white board that would do just that. I've seen samples but forgot the details. Those interview questions are tough.

David
 
I'm not sure how to handle this as an array or function.
Maybe, you can help? I know it's ugly but I'm trying to meet a tight deadline.
tia,
David

// Validates Date Fields -- works but needs refactoring...

try
{

string input = txtProductionDate.Text;

int year = int.Parse(input.Substring(0, 4));
int month = int.Parse(input.Substring(5, 2));
int day = int.Parse(input.Substring(8, 2));

DateTime parseDate = new DateTime(year, month, day);


if(1 < 2)
{



//Response.Write(parseDate.Year);
//Response.Write(parseDate.Month);
//Response.Write(parseDate.Day);



if (parseDate.Year.ToString() != "" || parseDate.Year.ToString() != null)
{
input = "Date is Valid!";
}

}
string input2 = txtDate.Text;

year = int.Parse(input2.Substring(0, 4));
month = int.Parse(input2.Substring(5, 2));
day = int.Parse(input2.Substring(8, 2));

DateTime parseDate2 = new DateTime(year, month, day);
if (1 < 2)
{



//parseDate = new DateTime(year, month, day);

//Response.Write(parseDate2.Year);
//Response.Write(parseDate2.Month);
//Response.Write(parseDate2.Day);



if (parseDate2.Year.ToString() != "" || parseDate2.Year.ToString() != null)
{
//input2 = "Date is Valid!";
}

}
string input3 = txtLicStart.ToString();

year = int.Parse(input3.Substring(0, 4));
month = int.Parse(input3.Substring(5, 2));
day = int.Parse(input3.Substring(8, 2));

DateTime parseDate3 = new DateTime(year, month, day);
if (1 < 2)
{


//parseDate = new DateTime(year, month, day);

//Response.Write(parseDate2.Year);
//Response.Write(parseDate2.Month);
//Response.Write(parseDate2.Day);



if (parseDate3.Year.ToString() != "" || parseDate3.Year.ToString() != null)
{
//input2 = "Date is Valid!";
}

}
string input4 = txtLicEnd.ToString();

year = int.Parse(input4.Substring(0, 4));
month = int.Parse(input4.Substring(5, 2));
day = int.Parse(input4.Substring(8, 2));

DateTime parseDate4 = new DateTime(year, month, day);
if (1 < 2)
{

//parseDate = new DateTime(year, month, day);

//Response.Write(parseDate2.Year);
//Response.Write(parseDate2.Month);
//Response.Write(parseDate2.Day);



if (parseDate4.Year.ToString() != "" || parseDate4.Year.ToString() != null)
{
//input2 = "Date is Valid!";
}

}
}


catch (Exception ertp)
{
//Console.Write(ex.StackTrace);
Response.Write("<span class=\"err\">");
Response.Write( "Please, check one of the Date fields for an error.<br/> ");
Response.Write("Dates should be entered like this:");
Response.Write(" 2007-02-14 (year, month, day)<br/> ");
Response.Write(ertp.Source + ", <br/>");
Response.Write(ertp.StackTrace + ", <br/>");
Response.Write(ertp.Message);
Response.Write("</span><br/> <br/> ");
//Response.Redirect("viewfolder.aspx");

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top