I'm using a CustomValidator field in an Asp.net Webform to validate a date field. How can I validate multiple date formats? For example, this would be valid: 01/05/2019 and this would be valid: 1/05/2019 and this would be valid: 2019/05/01. Here is the code behind that doesn't work when trying to validate all three date types but does work when using only one date format:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
//Issue Date
if (Regex.IsMatch(txtIssueDate.Text, "(((0|1)[0-9]|2[0-9]|3[0-1])\\/(0[1-9]|1[0-2])\\/((19|20)\\d\\d))$"))
{
DateTime dt;
args.IsValid = DateTime.TryParseExact(args.Value, "dd/MM/yyyy", new CultureInfo("en-GB"), DateTimeStyles.None, out dt);
args.IsValid = DateTime.TryParseExact(args.Value, "d/MM/yyyy", new CultureInfo("en-GB"), DateTimeStyles.None, out dt);
args.IsValid = DateTime.TryParseExact(args.Value, "yyyy/MM/dd", new CultureInfo("en-GB"), DateTimeStyles.None, out dt);
if (args.IsValid)
{
args.IsValid = true;
}
}
else
{
args.IsValid = false;
ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "myFunction", "alertInvalidDate();", true);
}
}
EDIT I took Sach's advice and used this method to check the date fields - forgetting the Custom Validator altogether:
var formats = new string[] { "dd/MM/yyyy", "d/MM/yyyy", "yyyy/MM/dd" };
var isValidFormat = DateTime.TryParseExact(txtIssueDate.Text, formats, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime res1);
if (isValidFormat)
{
//It's all good.
}
else
{
ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "myFunction", "alertInvalidDate();", true);
}
You just want to use TryParseExact()
with multiple formats.
static void Main()
{
var formats = new string[] { "dd/MM/yyyy", "d/MM/yyyy", "yyyy/MM/dd" };
DateTime.TryParseExact("01/05/2019", formats, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime res1);
Console.WriteLine(res1.ToLongDateString());
DateTime.TryParseExact("1/05/2019", formats, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime res2);
Console.WriteLine(res2.ToLongDateString());
DateTime.TryParseExact("2019/05/01", formats, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime res3);
Console.WriteLine(res3.ToLongDateString());
Console.ReadLine();
}
Wednesday, May 01, 2019
Wednesday, May 01, 2019
Wednesday, May 01, 2019
Your code doesn't work because it takes into account the result of the last TryParseExact
line ONLY :
args.IsValid = DateTime.TryParseExact(args.Value, "yyyy/MM/dd", new CultureInfo("en-GB"), DateTimeStyles.None, out dt);
Previous results are lost.
Also, this piece is completely meaninglessness ( I hope you understand why ):
if (args.IsValid)
{
args.IsValid = true;
}
Try this instead:
if (Regex.IsMatch(txtIssueDate.Text, "(((0|1)[0-9]|2[0-9]|3[0-1])\\/(0[1-9]|1[0-2])\\/((19|20)\\d\\d))$"))
{
DateTime dt;
args.IsValid = (DateTime.TryParseExact(args.Value,
"dd/MM/yyyy",
new CultureInfo("en-GB"),
DateTimeStyles.None,
out dt) ||
DateTime.TryParseExact(args.Value,
"d/MM/yyyy",
new CultureInfo("en-GB"),
DateTimeStyles.None,
out dt) ||
DateTime.TryParseExact(args.Value,
"yyyy/MM/dd",
new CultureInfo("en-GB"),
DateTimeStyles.None,
out dt));
}
else
{
args.IsValid = false;
ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "myFunction", "alertInvalidDate();", true);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.