Regular validation date format JS code

Source: Internet
Author: User

YYYY-MM-DD format

The following are:

The code is as follows Copy Code
/^d{4}-d{1,2}-d{1,2}$/

4 digits, horizontal lines, 1 or 2 digits, a horizontal line, and finally 1 or 2 digits.

The test code is as follows:

  code is as follows copy code

 < Script type= "Text/javascript" >
 function testreg (reg,str) {
  return reg.test (str);
 }
 var reg =/^d{4}-d{1,2}-d{1,2}$/;
 var str = ' 2008-8-8 ';
 var str2 = ' 2008-08-08 ';
 var str3 = ' 08-08-2008 ';
 var str4 = ' 2008 08 08 ';
 document.write (Testreg (reg,str) + ' <br/> ');
 document.write (Testreg (REG,STR2) + ' <br/> ');
 document.write (Testreg (REG,STR3) + ' <br/> ');
 document.write (Testreg (REG,STR4) + ' <br/> ');
 </script>


Second Kind

The code is as follows Copy Code

Yyyy-mm-dd
Or
Yyyy/mm/dd

Just use the "or" to simply change the line.

The code is as follows Copy Code

/^D{4} (-|/) d{1,2} (-|/) d{1,2}$/

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.