JS validation date format various code

Source: Internet
Author: User
Tags getdate

Example 1

Verify YYYY-MM-DD date format, do not validate incorrect dates such as "2012-11-45"

The code is as follows Copy Code


<script language= "JavaScript" >
function Formattime (str)
{

var r = Str.match (/^ (d{1,4}) (-|/) (d{1,2}) 2 (d{1,2}) $/);
if (r==null) return false;
var d= new Date (r[1], r[3]-1, r[4]);
Return (D.getfullyear () ==r[1]&& (D.getmonth () +1) ==r[3]&&d.getdate () ==r[4]);

}

function Checkdata (cform) {
if (document.vbform.bdaynew.value== "")
{
Alert ("The due date cannot be empty, please enter!") ");
Document.vbform.bdaynew.focus ();
return false;
}
if (!formattime (Cform.bdaynew.value))
{
Alert ("Up to date format error!") ");
Cform.bdaynew.focus ();
return false;
}

return true;
}

</script>

<form name= "Vbform" onsubmit= "Return to Checkdata (this);" >

<input name= "bdaynew" type= "text" size= "" class= "Input" >

<span style= "color: #FF0000" > Time format is 2007-01-01 or 2007-1-1</span>

</form>

If I were in the 2013/01/01 format,

The code is as follows Copy Code


Determine if input is a date
function isdatestring (sdate)
{
var mp=/d{4}-d{2}-d{2}/;

   var Matcharray = Sdate.match (MP);
   if (matcharray==null) return false;
   var iamonthdays = [31,28,31,30,31,30,31,31,30,31,30,31];
   var iadate = new Array (3);
   var year, month, day; 
   
    iadate = Sdate.split ("/"); &N bsp; //Here we can automatically modify-can also be: Oh.  
    year = parsefloat (iadate[0])
    month = parsefloat (iadate[1])
&NB sp;   day=parsefloat (iadate[2])
    if (Year < 1900 | | year > 2100) return false;
    if ((year% 4 = 0) && (year% 100!= 0) | | (Year% 400 = 0)) iamonthdays[1]=29;
    if (Month < 1 | | month >) return false;
    if (Day < 1 | | day > IAMONTHDAYS[MONTH-1]) return false;
    return true;
}

Use
if (!isdatestring (complete_date))
{
Alert ("The actual finish date is not in the correct format and the date format is Yyyy-mm-dd");
Return
}


Long time, shaped like (2003-12-05 13:04:06)

The code is as follows Copy Code

function Strdatetime (str)

       {

          var reg =/^ (d{1,4}) (-|/) (d{1,2}) 2 (d{1,2}) (d{1,2}):(d{1,2}):(d{1,2}) $/;

         var r = Str.match (reg);

         if (r==null) return false;

         var d= new Date (r[1], r[3]-1,r[4],r[5],r[6],r[7]);

         return (D.getfullyear () ==r[1]&& (D.getmonth () +1) ==r[3 ]&&d.getdate () ==r[4]&&d.gethours () ==r[5]&&d.getminutes () ==r[6]&&d.getseconds () ==R[7]);

}

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.