Validation date-Javascript

Source: Internet
Author: User

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:

<Asp: textbox id = "m_txtdate" runat = "server" onblur = "validatedate ('m _ txtdate ')"> </ASP: textbox>

Add this code in your ASPX page. hope this works! Cheers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.