更新时间:2023-02-26 20:43:50
我不会使用正则表达式:它很难做到正确,并处理适当月份的天数。
相反,看看使用DateTime.TryParseExact:DateTime dt;
if (DateTime.TryParseExact( 12 -Oct-2011 22:34, dd-MMM-yyyy HH:mm,CultureInfo.InvariantCulture,DateTimeStyles.None, out dt))
{
Console.WriteLine(dt);
}
试试这个\d {2} / \d {2} / \d {4}转到正则表达式在这里写一个表达式:validate expression \d {2} / \d {2} / \d {4}
这将验证它为dd / mm / yyyy
为我的解决方案我做了这个正则表达式以验证此格式中的日期时间dd-MMM-yyyy HH:mm
这是正则表达式使用^(0?[1-9 ] | [12] [0-9] | 3 [01]) - (月|月| JAN | 2月| 2月| 2月| 3月| 3月| MAR |四月|四月|四月|可能会|五月|五月|六月|君| JUN |七月|七月| JUL |八月|八月| AUG |九月|九月| SEP |十月|十月|华侨城| 11月| 11月| 11月| 12月| 12月| DEC) - (19 | 20)\d\\ \\d\s([0-1] [0-9] | [2] [0-3]):([0-5] [0-9])
I have one textbox and also one MaskedEditExtender to attache with that text box for entering value in this formate like dd-MMM-yyyy HH:mm....
now i want to put one RegularExpressionValidator to validate that text box.....
i have put it like this...
<asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("ABOVEROTARYTABLE", "{0:d-MMM-yyyy HH:mm}") %>'>
<cc1:MaskedEditExtender ID="MaskedEditExtender4" runat="server" ClearMaskOnLostFocus="False"
CultureName="en-GB" Mask="99-LLL-9999 99:99" TargetControlID="TextBox9" UserDateFormat="DayMonthYear">
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="TextBox9"
ErrorMessage="Invalid DateTime"
ValidationGroup="GridViewBHAHistory"
ValidationExpression=""
now i want regular expression to validate my input.....
Valid like
12-Oct-2011 22:34
23-dec-1988 02:09
or not valid like
43-TTT-8888 88:98
12-oct-2011 25:39
help me....
I wouldn't use a Regex for this: it is too difficult to get it right, and handle the number of days in the appropriate month.
Instead, look at using DateTime.TryParseExact:DateTime dt; if (DateTime.TryParseExact("12-Oct-2011 22:34", "dd-MMM-yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt)) { Console.WriteLine(dt); }
try this \d{2}/\d{2}/\d{4} go to the regular expression properties and in that write a expression in:validate expression \d{2}/\d{2}/\d{4}
this will validate it to dd/mm/yyyy
for my solution i made this regular expression to validate datetime in this formatedd-MMM-yyyy HH:mm
this is the regular expression use"^(0?[1-9]|[12][0-9]|3[01])-(jan|Jan|JAN|feb|Feb|FEB|mar|Mar|MAR|apr|Apr|APR|may|May|MAY|jun|Jun|JUN|jul|Jul|JUL|aug|Aug|AUG|sep|Sep|SEP|oct|Oct|OCT|nov|Nov|NOV|dec|Dec|DEC)-(19|20)\d\d\s([0-1][0-9]|[2][0-3]):([0-5][0-9])