求一个 正则表达式, 谢谢 如题, 要求如下:要求验证年月1. 一定要6位长度,例如 2008042. 前四位,要大于2000,小于30003. 最后两位,要大于等于01, 小于等于12谢谢各位了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.java2000.net/viewthread.jsp?tid=471日期的完全判断 http://www.java2000.net/viewthread.jsp?tid=471 String date="2008-04-11"; (这个date可以是你从数据库中获得的值)date.substring(0,7);System.out.println(date.substring(0,6); 结果 2008-04 要200804也可以啊date.substring(0,4);date.substring(6,7);String date1=date.substring(0,4)+date.substring(5,7);System.out.println(date1); 结果 200804datex=date.substring(0,4);datey=date.substring(8,10);判断前四为大于2000也没有问题啊数据库中查询语句 select * from (table name) where (datex) >2000 and (datex)<3000 and (datey)>=01 and (datey)<=12你自己尝试用下substring 截取字符串应该不难出结果 在JS用不着用正则,可以直接用substring或者substr方法截取后比较就行了~ public class RTimeTest {public static void main(String[] args){ String regular="2\\d{3}(0[1-9]|1[0-2])"; String test1="209001"; String test2="300012"; String test3="200913"; System.out.println(test1.matches(regular)); System.out.println(test2.matches(regular)); System.out.println(test3.matches(regular)); }}结果truefalsefalse 这个函数完全可以实现你要求的功能了,已经测试过,呵呵return了值是为了你如果是用在SUBMIT的情况下写的~<html><head><script type="text/javascript">function checkDate(){ var date = document.getElementById("test").value; if(date.length != "6" || !date.match(/^\d+$/))//如需校验输入是否为数字,可以用"^\d+$"正则判断一下 { alert("输入值长度应该为6的数字"); return false; } else{ var year = date.substr(0,4); var month = date.substr(4,2); if(year < 2000 || year > 3000) { alert("输入的年份要在2000到3000之间"); return false; } if(month < 01 || month > 12) { alert("输入的月份要在01到12之间"); return false; } return true; } } </script> </head><body><form><table><tr><td><input id="test" type="text" name="ss" value=""></td><td><input type="button" name="sub" value="提交" onclick="checkDate()"></td></tr></table></form></body></html> 郁闷,没注意LZ是在java中用还是在JS中用.....在java中用2\\d{3}(0[1-9]|1[0|1|2])就行了,10楼的已经给出答案了,不好意思~~呵呵 用ftpClient下载FTP上的文件报错问题 为什么用request.setAttribute传值取出来的是null jsp编程新手买那些书好呢 el表达式的foreach 显示自定义类的问题 研究Alfresco 一个struts action问题 求救!!DB可以连接,但是不能查询和更新,好郁闷!!! 请问 用 java 如何 设置 日志中的 referer和useragen 值 急啊!关于jsp通过javabean连接数据库进行用户或企业登陆的问题 CXF3.0 的错误 html href 指向文件夹问题 请教 请高手指点!(JDBC技术)
日期的完全判断
date.substring(0,7);
System.out.println(date.substring(0,6); 结果 2008-04
要200804也可以啊
date.substring(0,4);
date.substring(6,7);
String date1=date.substring(0,4)+date.substring(5,7);
System.out.println(date1); 结果 200804
datex=date.substring(0,4);
datey=date.substring(8,10);
判断前四为大于2000也没有问题啊
数据库中查询语句 select * from (table name) where (datex) >2000 and (datex)<3000 and (datey)
>=01 and (datey)<=12你自己尝试用下substring 截取字符串应该不难出结果
public class RTimeTest
{
public static void main(String[] args)
{
String regular="2\\d{3}(0[1-9]|1[0-2])";
String test1="209001";
String test2="300012";
String test3="200913";
System.out.println(test1.matches(regular));
System.out.println(test2.matches(regular));
System.out.println(test3.matches(regular));
}
}
结果
true
false
false
<html>
<head>
<script type="text/javascript">
function checkDate()
{
var date = document.getElementById("test").value;
if(date.length != "6" || !date.match(/^\d+$/))//如需校验输入是否为数字,可以用"^\d+$"正则判断一下
{
alert("输入值长度应该为6的数字");
return false;
}
else{
var year = date.substr(0,4);
var month = date.substr(4,2);
if(year < 2000 || year > 3000)
{
alert("输入的年份要在2000到3000之间");
return false;
}
if(month < 01 || month > 12)
{
alert("输入的月份要在01到12之间");
return false;
}
return true;
}
} </script>
</head>
<body>
<form>
<table>
<tr>
<td><input id="test" type="text" name="ss" value=""></td>
<td><input type="button" name="sub" value="提交" onclick="checkDate()"></td>
</tr>
</table>
</form>
</body>
</html>
在java中用
2\\d{3}(0[1-9]|1[0|1|2])
就行了,10楼的已经给出答案了,不好意思~~呵呵