Forums » .NET » .NET »

How to validate a date?


Posted Date: 12 Apr 2004      Posted By:: Aisha     Member Level: Bronze  Member Rank: 0     Points: 2   Responses: 12

How to validate the date,both the month and the year, entered by a user?


Responses

#291    Author: ManojRajan      Member Level: Gold      Member Rank: 0     Date: 12/Apr/2004   Rating: 2 out of 52 out of 5     Points: 2

hi
If you can explain your requirement.

ManojRajan


 
#292    Author: ManojRajan      Member Level: Gold      Member Rank: 0     Date: 12/Apr/2004   Rating: 2 out of 52 out of 5     Points: 2

HI,

I assume, that you have to store the date in the database and hence you need a validation. One of the easiest way of doing this is to have a sub in which you can assign the day,month and year to a date type variable. And include this code in the try section and catch the exception.


This is how you call the function CheckDate("2", "12", "2004")


Private Sub CheckDate(ByVal d As String, ByVal m As String, ByVal y As String)
Try
Dim mydate As Date
mydate = New DateTime(y, m, d)
Catch obj As Exception
Response.Write(obj.Message)
End Try
End Sub


 
#296    Author: Ajay Singala      Member Level: Silver      Member Rank: 0     Date: 12/Apr/2004   Rating: 2 out of 52 out of 5     Points: 2

Aisha,

What exactly is the validation that you want to do? Validate the month and year against what? If you could post some more details, it would be great. Thanks.

Regards,

Ajay Singala [Synergeticsd].
.NET MVP.





 
#378    Author: Lalitha Maheswaran      Member Level: Bronze      Member Rank: 0     Date: 27/Apr/2004   Rating: 2 out of 52 out of 5     Points: 2

Hi,

Validation of date involves:

1. Check if the year is a leap year or not
2. Get the number of days in the user specified month in that year (as the number of days vary per month...also for feb if it is a leap year it is 29 else 28)
3. Check if the day entered is valid.

An year is a leap year if

it is divisible by 4
if it is divisible by 100, then it should be divisible by 400 also.

The following function in VBScript validates a date in dd/mm/yyyy format. The arrays aMonth and aLeapMonth specify the days in months of a normal, leap years correspondingly. That is aMonth(0) represents days in Jan month of a normal year.




'This function checks whether the date is valid
'It should check if the year is leap year and check if the date selected is
'valid
private function ValidateDate(sDate)
dim aMonth
dim aLeapMonth
dim aDateOfBirth
dim i, iDay, iMonth, iYear

ValidateDate = false

'Days in month of a normal year
aMonth = Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

'Days in month of a leap year
aLeapMonth = Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

'Get the day, month and year
aDateOfBirth = split(sDate, "/")
iDay = cint(aDateOfBirth(0))
iMonth = cint(aDateOfBirth(1))
iYear = cint(aDateOfBirth(2))

'If all the items - day, month, year are selected, do the validation....else return
'Note : The month array starts from 0
if (iDay <> 0) and (iMonth <> 0) and (iYear <> 0) then
if ((iYear mod 4) = 0) then
if (((iYear mod 100) = 0) and ((iYear mod 400) <> 0)) then 'Normal year
'Check if the day entered is valid
if (iDay) <= aMonth(iMonth - 1) then ValidateDate = true
else 'Leap year
if (iDay) <= aLeapMonth(iMonth - 1) then ValidateDate = true
end if
else 'Normal year
if (iDay) <= aMonth(iMonth - 1) then ValidateDate = true
end if
end if

end function


Hope this helps.

Regards,
Lalitha


 
#3849    Author: Dmitri Hoffmeyer      Member Level: Bronze      Member Rank: 0     Date: 10/Sep/2004   Rating: 2 out of 52 out of 5     Points: 2

Does this catch anything that ManojRajan's Try/Catch method wouldn't catch?

 
#73108    Author: poojitha      Member Level: Bronze      Member Rank: 0     Date: 01/Sep/2006   Rating: 2 out of 52 out of 5     Points: 2

Simple regular expression based function to validate date.
-----------------------------------------------------------------------------------
public bool isDate(string strDate)
{
string strRegex = @"^\d{1,2}\/\d{1,2}\/\d{2,4}$";
Regex re = new Regex(strRegex);
if (re.IsMatch(strDate))
return (true);
else
return (false);
}
-----------------------------------------------------------------------------------


 
#73119    Author: poojitha      Member Level: Bronze      Member Rank: 0     Date: 01/Sep/2006   Rating: 2 out of 52 out of 5     Points: 2

c# Date validator function with Leap Year handling
-------------------------------------------------------------------------------------------------------------
public bool isDate(string strDate)
{
string strRegex = @"((^(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])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(0?2)([/])(29)([/])([2468][048]00)$)|(^(0?2)([/])(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])$))";

Regex re = new Regex(strRegex);
if (re.IsMatch(strDate))
return (true);
else
return (false);
}

This function will validate any date from 1800 -to 9999.
Also It will handle leap years.
Allowed formats are mm/dd/yyyy , m/dd/yyyy , mm/d/yyyy , m/d/yyyy.


 
#107387    Author: Saw Qas      Member Level: Bronze      Member Rank: 0     Date: 31/Jan/2007   Rating: 2 out of 52 out of 5     Points: 2

Thank you Pujitha Sendanayake, your solution was really helpfull.

 
#107389    Author: Saw Qas      Member Level: Bronze      Member Rank: 0     Date: 31/Jan/2007   Rating: 2 out of 52 out of 5     Points: 2

Thank you Pujitha Sendanayake, your solution was really helpfull.
It just needed fixing for the problems with the regular expression it uses to ... ArgumentException: parsing "[=-\+]" - [x-y] range in reverse order.
And the solution is to escape the [\-] in the expression. So your strRegex should be:
string strRegex = @"((^(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])([/])((1[8\-9]\d{2})|([2\-9]\d{3}))$)|(^(0?2)([/])(29)([/])([2468][048]00)$)|(^(0?2)([/])(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])$))";

Thanks again


 
#139747    Author: p1gsy      Member Level: Bronze      Member Rank: 0     Date: 26/Jul/2007   Rating: 2 out of 52 out of 5     Points: 2

alternatively this is easier

private bool isDate(string strDate)
{
try
{
DateTime.Parse(strDate);
return true;
}
catch(FormatException e)
{
return false;
}
}


 
#209691    Author: Raghuramakarthikeyan      Member Level: Gold      Member Rank: 0     Date: 03/Apr/2008   Rating: 2 out of 52 out of 5     Points: 2

there is Date sontrols are there you may use that instead of validating

 
#213919    Author: senthil      Member Level: Silver      Member Rank: 0     Date: 11/Apr/2008   Rating: 2 out of 52 out of 5     Points: 2

There is no need to validate the date if you use this code


<html>
<head>
<title>Date</title>
<script >

function listdate(mon,year)
{

var a=year%4;

if(mon==04||mon==06||mon==09|mon==11)
{
var output="<select name='date'><OPTION value='00'>dd</OPTION><OPTION value='01'>01</OPTION><OPTION value='02'>02</OPTION><OPTION value='03'>03</OPTION><OPTION value='04'>04</OPTION><OPTION value='05'>05</OPTION><OPTION value='06'>06</OPTION><OPTION value='07'>07</OPTION><OPTION value='08'>08</OPTION><OPTION value='09'>09</OPTION><OPTION value='10'>10</OPTION><OPTION value='11>11</OPTION><OPTION value='12'>12</OPTION><OPTION value='13'>13</OPTION><OPTION value='14'>14</OPTION><OPTION value='15'>15</OPTION><OPTION value='16'>16</OPTION><OPTION value='17'>17</OPTION><OPTION value='18'>18</OPTION><OPTION value='19'>19</OPTION><OPTION value='20'>20</OPTION><OPTION value='21'>21</OPTION><OPTION value='22'>22</OPTION><OPTION value='23'>23</OPTION><OPTION value='24'>24</OPTION><OPTION value='25'>25</OPTION><OPTION value='26'>26</OPTION><OPTION value='27'>27</OPTION><OPTION value='28'>28</OPTION><OPTION value='29'>29</OPTION><OPTION value='30'>30</OPTION></SELECT>"

document.getElementById("dateselDIV").innerHTML=output;
}
if(mon==02)
{
if(a==0){
var output="<select name='date'><OPTION value='0'>dd</OPTION><OPTION value='1'>01</OPTION><OPTION value='2'>02</OPTION><OPTION value='3'>03</OPTION><OPTION value='4'>04</OPTION><OPTION value='5'>05</OPTION><OPTION value='6'>06</OPTION><OPTION value='7'>07</OPTION><OPTION value='8'>08</OPTION><OPTION value='9'>09</OPTION><OPTION value='10'>10</OPTION><OPTION value='11>11</OPTION><OPTION value='12'>12</OPTION><OPTION value='13'>13</OPTION><OPTION value='14'>14</OPTION><OPTION value='15'>15</OPTION><OPTION value='16'>16</OPTION><OPTION value='17'>17</OPTION><OPTION value='18'>18</OPTION><OPTION value='19'>19</OPTION><OPTION value='20'>20</OPTION><OPTION value='21'>21</OPTION><OPTION value='22'>22</OPTION><OPTION value='23'>23</OPTION><OPTION value='24'>24</OPTION><OPTION value='25'>25</OPTION><OPTION value='26'>26</OPTION><OPTION value='27'>27</OPTION><OPTION value='28'>28</OPTION><OPTION value='29'>29</OPTION></SELECT>"

document.getElementById("dateselDIV").innerHTML=output;

}
else
{
var output="<select name='date'><OPTION value='0'>dd</OPTION><OPTION value='1'>01</OPTION><OPTION value='2'>02</OPTION><OPTION value='3'>03</OPTION><OPTION value='4'>04</OPTION><OPTION value='5'>05</OPTION><OPTION value='6'>06</OPTION><OPTION value='7'>07</OPTION><OPTION value='8'>08</OPTION><OPTION value='9'>09</OPTION><OPTION value='10'>10</OPTION><OPTION value='11>11</OPTION><OPTION value='12'>12</OPTION><OPTION value='13'>13</OPTION><OPTION value='14'>14</OPTION><OPTION value='15'>15</OPTION><OPTION value='16'>16</OPTION><OPTION value='17'>17</OPTION><OPTION value='18'>18</OPTION><OPTION value='19'>19</OPTION><OPTION value='20'>20</OPTION><OPTION value='21'>21</OPTION><OPTION value='22'>22</OPTION><OPTION value='23'>23</OPTION><OPTION value='24'>24</OPTION><OPTION value='25'>25</OPTION><OPTION value='26'>26</OPTION><OPTION value='27'>27</OPTION><OPTION value='28'>28</OPTION></SELECT>"

document.getElementById("dateselDIV").innerHTML=output;

}

}
if(mon==01||mon==03||mon==05||mon==07||mon==08|mon==10||mon==12)
{

var output="<select name='date'><OPTION value='0'>dd</OPTION><OPTION value='1'>01</OPTION><OPTION value='2'>02</OPTION><OPTION value='3'>03</OPTION><OPTION value='4'>04</OPTION><OPTION value='5'>05</OPTION><OPTION value='6'>06</OPTION><OPTION value='7'>07</OPTION><OPTION value='8'>08</OPTION><OPTION value='9'>09</OPTION><OPTION value='10'>10</OPTION><OPTION value='11>11</OPTION><OPTION value='12'>12</OPTION><OPTION value='13'>13</OPTION><OPTION value='14'>14</OPTION><OPTION value='15'>15</OPTION><OPTION value='16'>16</OPTION><OPTION value='17'>17</OPTION><OPTION value='18'>18</OPTION><OPTION value='19'>19</OPTION><OPTION value='20'>20</OPTION><OPTION value='21'>21</OPTION><OPTION value='22'>22</OPTION><OPTION value='23'>23</OPTION><OPTION value='24'>24</OPTION><OPTION value='25'>25</OPTION><OPTION value='26'>26</OPTION><OPTION value='27'>27</OPTION><OPTION value='28'>28</OPTION><OPTION value='29'>29</OPTION><OPTION value='30'>30</OPTION><option value='31'>31</option></SELECT>"
document.getElementById("dateselDIV").innerHTML=output;
}
}

</script>
</head>
<body>

<form name="RegistrationForm">
<table>
<tr>
<td>Date of Birth:<span class='style3'>*</span></td>

<td><select name="year" onchange="listdate(document.RegistrationForm.month.value,this.value)">
<option value="0">year</option>

<option value="1990">1990</option>
<option value="1989">1989</option>
<option value="1988">1988</option>
<option value="1987">1987</option>
<option value="1986">1986</option>
<option value="1985">1985</option>
<option value="1984">1984</option>
<option value="1983">1983</option>
<option value="1982">1982</option>
<option value="1981">1981</option>
<option value="1980">1980</option>
<option value="1979">1979</option>
<option value="1978">1978</option>
<option value="1977">1977</option>
<option value="1976">1976</option>
<option value="1975">1975</option>
<option value="1974">1974</option>
<option value="1973">1973</option>
<option value="1972">1972</option>
<option value="1971">1971</option>
<option value="1970">1970</option>
<option value="1969">1969</option>
<option value="1968">1968</option>
<option value="1967">1967</option>
<option value="1966">1966</option>
<option value="1965">1965</option>
<option value="1964">1964</option>
<option value="1963">1963</option>
<option value="1962">1962</option>
<option value="1961">1961</option>
<option value="1960">1960</option>
<option value="1959">1959</option>
<option value="1958">1958</option>
<option value="1957">1957</option>
<option value="1956">1956</option>
<option value="1955">1955</option>
<option value="1954">1954</option>
<option value="1953">1953</option>
<option value="1952">1952</option>
<option value="1951">1951</option>
<option value="1950">1950</option>
<option value="1949">1949</option>
<option value="1948">1948</option>
<option value="1947">1947</option>
<option value="1946">1946</option>
<option value="1945">1945</option>
<option value="1944">1944</option>
<option value="1943">1943</option>
<option value="1942">1942</option>
<option value="1941">1941</option>
<option value="1940">1940</option>
<option value="1939">1939</option>
<option value="1938">1938</option>
<option value="1937">1937</option>
<option value="1936">1936</option>
<option value="1935">1935</option>
<option value="1934">1934</option>
<option value="1933">1933</option>
<option value="1932">1932</option>
<option value="1931">1931</option>
<option value="1930">1930</option>
<option value="1929">1929</option>
<option value="1928">1928</option>
<option value="1927">1927</option>
<option value="1926">1926</option>
<option value="1925">1925</option>
<option value="1924">1924</option>
<option value="1923">1923</option>
<option value="1922">1922</option>
<option value="1921">1921</option>
<option value="1920">1920</option>
<option value="1919">1919</option>
<option value="1918">1918</option>
<option value="1917">1917</option>
<option value="1916">1916</option>
<option value="1915">1915</option>
<option value="1914">1914</option>
<option value="1913">1913</option>
<option value="1912">1912</option>
<option value="1911">1911</option>
<option value="1910">1910</option>
<option value="1909">1909</option>
<option value="1908">1908</option>
<option value="1907">1907</option>
<option value="1906">1906</option>
<option value="1905">1905</option>
<option value="1904">1904</option>
<option value="1903">1903</option>
<option value="1902">1902</option>
<option value="1901">1901</option>
<option value="1900">1900</option>
</select> <select name="month" onchange="listdate(this.value,document.RegistrationForm.year.value)">
<option value="0">mon</option>
<option value="01">jan</option>
<option value="02">feb</option>
<option value="03">mar</option>
<option value="04">apr</option>
<option value="05">may</option>
<option value="06">jun</option>
<option value="07">jul</option>
<option value="08">aug</option>
<option value="09">sep</option>
<option value="10">oct</option>
<option value="11">nov</option>
<option value="12">dec</option>
</select><span id="dateselDIV">
<select name="date">
<option>dd</option>
</select> </span>
</td>
</tr></table></form>
</body></html>



 
Post Reply     

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.





  • Return to Discussion Forum
  • Start new thread


  • Top Contributors
    Today
      Last 7 Days
        more...

        Awards & Gifts

        Online Members

        Shanmugam
        More...
         
        Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India