http://social.msdn.microsoft.com/forums/en-US/showandtell/thread/e2a94c9b-96a4-4d8f-ae85-cf242cd45afa/

 

This a UK style of date validation. This can be changed to cater US style as well. Add this script in the HEAD tag of HTML Page

 <SCRIPT type=text/javascript>
//This function is used to validate date in the format dd/mm/yyyy (U.K. style)
//dtControl is the client id of the control which contains the date

function validateDate(dtControl) 
{
    var input = document.getElementById(dtControl)
    var validformat=/^\d{1,2}\/\d{1,2}\/\d{4}$/ //Basic check for format validity
    var returnval=false
    if (!validformat.test(input.value))
    alert('Invalid Date Format. Please correct.')
    else{ //Detailed check for valid date ranges
    var dayfield=input.value.split("/")[0]
    var monthfield=input.value.split("/")[1]
    var yearfield=input.value.split("/")[2]
    
    var dayobj = new Date(yearfield, monthfield-1, dayfield)
    if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
    alert('Invalid Day, Month, or Year range detected. Please correct.')
    else
    {
        returnval=true
    }
    }
    if (returnval==false) input.focus()
    return returnval

</SCRIPT>

This can be set to work on client side now. For example, use this function on blur of a text box as:

<asp:TextBox ID="m_txtDate" runat="server" onBlur="validateDate('m_txtDate')"></asp:TextBox>

Add this code in your aspx page. Hope this works! Cheers

posted on 2011-11-03 09:19  Black Bean  阅读(222)  评论(0编辑  收藏  举报