---
title: "正则表达式 日期判断(java js)"
author: "yangy608"
date: "2010-03-08T17:52:21Z"
tags: []
categories: ["编程语言"]
slug: "java-js"
draft: false
lastmod: "2010-03-10T09:24:55Z"
id: 610643
url: "https://yangy608.iteye.com/blog/610643"
---

```
public class Test {

 public static void main(String[] args) {
  
  //匹配 Unit price   ^0\\.[0-9]*$
  //匹配 Order qty   \\d*$


  if(Pattern.compile("(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)").matcher("1999-02-18").matches()){
   System.err.println("匹配的");
  }else{
   System.err.println("不匹配的");
  }

 }
}
```

 

 

 

YYYY-MM-DD的正则表达式为：  
((\[0-9]{3}\[1-9]|\[0-9]{2}\[1-9]\[0-9]{1}|\[0-9]{1}\[1-9]\[0-9]{2}|\[1-9]\[0-9]{3})-(((0\[13578]|1\[02])-(0\[1-9]|\[12]\[0-9]|3\[01]))|((0\[469]|11)-(0\[1-9]|\[12]\[0-9]|30))|(02-(0\[1-9]|\[1]\[0-9]|2\[0-8]))))|(((\[0-9]{2})(0\[48]|\[2468]\[048]|\[13579]\[26])|((0\[48]|\[2468]\[048]|\[3579]\[26])00))-02-29)

 

DD/MM/YYYY格式的正则验证表达式为：

(((0\[1-9]|\[12]\[0-9]|3\[01])/((0\[13578]|1\[02]))|((0\[1-9]|\[12]\[0-9]|30)/(0\[469]|11))|(0\[1-9]|\[1]\[0-9]|2\[0-8])/(02))/(\[0-9]{3}\[1-9]|\[0-9]{2}\[1-9]\[0-9]{1}|\[0-9]{1}\[1-9]\[0-9]{2}|\[1-9]\[0-9]{3}))|(29/02/((\[0-9]{2})(0\[48]|\[2468]\[048]|\[13579]\[26])|((0\[48]|\[2468]\[048]|\[3579]\[26])00)))

 

 

 

 

```
<input name="sj" type="text" id="sj" onblur="valid()" onfocus="view()" />
<script language="javascript">
var el=document.getElementById("sj");
function valid()
{
    var re=/(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29) /;
 if(!re.test(el.value))
    {
        alert('格式不正确');
    }
    else 
 {
  alert('格式正确啊');
    }
}
</script>
```