I have 3 text box fields. to represent a date
eg DD MM YYYY
how can i validate only correct data is entered into each text box. is it a regexpression??
i need to do this inside the ascx/aspx file rather than the .cs codebehind
thanks
I have 3 text box fields. to represent a date
eg DD MM YYYY
how can i validate only correct data is entered into each text box. is it a regexpression??
i need to do this inside the ascx/aspx file rather than the .cs codebehind
thanks
You should use CustomValidator to validate your result input of all 3 controls. Maybe inside that custom validation script you can use regex to validate data.
You could validate each field with regexes, but it wouldn't take into account different months with different numbers of days: you could enter invalid dates.
On the server side it could be validated with something like this:
DateTime D;
string CombinedDate=String.Format("{0}-{1}-{2}", YearField.Text, MonthField.Text, DayField.Text);
if(DateTime.TryParseExact(CombinedDate, "yyyy-M-d", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out D)) {
// valid
} else {
// not valid
}
Try putting them in to a DateTime objects.
int day, month, year;
if (Int32.TryParse(dayInput.Value, out day)) {
if (Int32.TryParse(monthInput.Value, out month)) {
if (Int32.TryParse(yearInput.Value, out year)) {
DateTime birthDate = new DateTime(year, month, day);
if ([birthDate is in valid range])
// it passes
}
}
}
I know this isn't very elegant, but you can also test it the same way using the following RegEx
[0-9]+
However I like my way because I can enter it in to a DateTime field and then test the range to make sure it doesn't exceed the current date and is not below a 100 years before the current date.
Wouldn't validation in the aspx file introduce logic code in the presentation layer?
I would suggest an AJAX control (there is an AJAX MaskEdit box - alike). They usually okay for these sort of things, look into the AJAX toolkit, if the server you're deploying can support those.
Full example:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Web_Layer.WebForm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<script runat="server">
protected void ValidateDate(object sender, EventArgs e)
{
int day=0;
int month=0;
int year=0;
if (!int.TryParse(txtDD.Text, out day))
day = 0;
if (!int.TryParse(txtMM.Text, out month))
month = 0;
if (!int.TryParse(txtYY.Text, out year))
year = 0;
if (((year > 0)) && ((month > 0) && (month < 13)) && ((day > 0) && (day <= DateTime.DaysInMonth(year, month))))
{
lblValid.Text = "Valid!";
}
else
{
lblValid.Text = "NOT Valid!";
}
}
</script>
<asp:TextBox ID="txtDD" runat="server"></asp:TextBox>
<asp:TextBox ID="txtMM" runat="server"></asp:TextBox>
<asp:TextBox ID="txtYY" runat="server"></asp:TextBox>
<asp:Button ID="btn" runat="server" OnClick="ValidateDate"/>
<asp:Label ID="lblValid" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
public bool isValidDate(string datePart, string monthPart, string yearPart)
{
DateTime date;
string strDate = string.Format("{0}-{1}-{2}", datePart, monthPart, yearPart);
if (DateTime.TryParseExact(strDate, "dd-MM-yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo , System.Globalization.DateTimeStyles.None, out date ))
{
return true;
}
else
{
return false ;
}
}