31 Mar 2017
30 Mar 2017
Validating Date Using C# in ASP.NET
Hi,
This function used to validate date using C#, here i declared DateValidate function and passing date to strDate parameter.
Ex: 21/11/2014 DD/MM/YYYY
public bool DateValidate(string strDate)
{
try
{
int day;
int mon;
int yr;
if (strDate.Contains("/") == false)
{
return false;
}
if (strDate.Length != 10)
{
return false;
}
string[] strArray = new string[3];
char[] splitter = { '/' };
strArray = strDate.Split(splitter);
day = Convert.ToInt16(strArray[0]);
mon = Convert.ToInt16(strArray[1]);
yr = Convert.ToInt16(strArray[2]);
if (day > 31)
{
return false;
}
if (mon > 12)
{
return false;
}
if (day <= 0)
{
return false;
}
if (mon <= 0)
{
return false;
}
if (strArray[2].Length < 4)
{
return false;
}
if (yr <= 1753)
{
return false;
}
if (yr >= 9999)
{
return false;
}
if (day > DateTime.DaysInMonth(yr, mon))
{
return false;
}
if (mon == 2)
{
if ((DateTime.IsLeapYear(yr) == true) && (day > 29))
{
return false;
}
else
{
if ((DateTime.IsLeapYear(yr) == false) && (day > 28))
{
return false;
}
}
}
return true;
}
catch
{
return false;
}
}
This function used to validate date using C#, here i declared DateValidate function and passing date to strDate parameter.
Ex: 21/11/2014 DD/MM/YYYY
public bool DateValidate(string strDate)
{
try
{
int day;
int mon;
int yr;
if (strDate.Contains("/") == false)
{
return false;
}
if (strDate.Length != 10)
{
return false;
}
string[] strArray = new string[3];
char[] splitter = { '/' };
strArray = strDate.Split(splitter);
day = Convert.ToInt16(strArray[0]);
mon = Convert.ToInt16(strArray[1]);
yr = Convert.ToInt16(strArray[2]);
if (day > 31)
{
return false;
}
if (mon > 12)
{
return false;
}
if (day <= 0)
{
return false;
}
if (mon <= 0)
{
return false;
}
if (strArray[2].Length < 4)
{
return false;
}
if (yr <= 1753)
{
return false;
}
if (yr >= 9999)
{
return false;
}
if (day > DateTime.DaysInMonth(yr, mon))
{
return false;
}
if (mon == 2)
{
if ((DateTime.IsLeapYear(yr) == true) && (day > 29))
{
return false;
}
else
{
if ((DateTime.IsLeapYear(yr) == false) && (day > 28))
{
return false;
}
}
}
return true;
}
catch
{
return false;
}
}
Adding dynamic CSS to List Item using MVC
Hi,
In this post I have explained
how to add dynamic css styles to list menu using MVC. Here controller name is "Home" and action name is "Index".
If user select this action then we are applying class "selected" to the list item.
<li @(ViewContext.RouteData.Values["actioin"].ToString().Equals("Index")
&& ViewContext.RouteData.Values["controller"].TOString().Equals("Home") ? "class=selected" : "")>
<a href="@Url.Action("Index" , "Home")"> home<a/>
</li>
In this post I have explained
how to add dynamic css styles to list menu using MVC. Here controller name is "Home" and action name is "Index".
If user select this action then we are applying class "selected" to the list item.
<li @(ViewContext.RouteData.Values["actioin"].ToString().Equals("Index")
&& ViewContext.RouteData.Values["controller"].TOString().Equals("Home") ? "class=selected" : "")>
<a href="@Url.Action("Index" , "Home")"> home<a/>
</li>
Subscribe to:
Posts (Atom)