A Date validation function

Source: Internet
Author: User

This article provides a date validation function is tested, the following many places using alert to prompt, because it is a test so give a hint, of course, if you want to change the alert to return to become, the following have marked.

<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> a date validation function </title>
<script>
function IsDate ()
{
var datestr = document.getElementById (' dates '). Value;
var Datepat =/^ (D{4}) (-) (d{1,2}) (-) (d{1,2}) $/;
var Matcharray = Datestr.match (Datepat);
if (Matcharray = null) alert (' Please enter Date ');//return false;
var month = matcharray[3];
var day = matcharray[5];
var year = matcharray[1];
if (Month < 1 | | | month >) alert (' Incorrect month '); You can change it to return false.
if (Day < 1 | | | |) alert (' incorrect date '); You can change it to return false.
if (month==4 | | month==6 | | month==9 | | month==11) && day==31) return false;
if (month = 2)
{
var isleap = (Year% 4 = 0 && (year%!= 0 | | year% 400 = 0));
if (Day > 29 | | (day==29 &&!isleap)) return false;
}
Alert (' right date '); You can change it to return true
}
</script>

<body>
<form id= "Form1" Name= "Form1" method= "Post" action= "" >
<label for= "TextField" ></label>
Enter Date (2010-11-03)
<input type= "text" name= "dates" id= "dates"/>
<input type= "button" name= "button" id= "button" value= "Test" onclick= "Web Effects: Return IsDate ();"/>
</form>
</body>

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.