做一个页面
验证一下日期输入格式:mm/dd/yyyy,alert出,日期中的 年。
用户名:字母开头的 英文
代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>日期</title>
</head>
<body>
<script type="text/javascript">
function testReg(reg,str){
return reg.test(str);
}
var reg = /^\d{4}\/[0]\d|[1][0-2]\/[0-2]\d|[3][0-1]$/;
var str = '2008/08/18';
var str2 = '2008/12/22';
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>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>日期</title>
</head>
<body>
<script type="text/javascript">
function testReg(reg,str){
return reg.test(str);
}
var reg = /^\d{4}\/[0]\d|[1][0-2]\/[0-2]\d|[3][0-1]$/;
var str = '2008/08/18';
var str2 = '2008/12/22';
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>
</body>
</html>
年份是四位数字\d{4},
月份是1-12月,我把他分为十月以内的月份,和十月以上的月份,就是[0]\d|[1][0-2],
日是1-31的数字,[0-2]\d|[3][0-1]
2.验证输入日期,如果正确alert年份
代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>日期用化名</title>
<script type="text/javascript" language="javascript" src="jquery.js"></script>
</head>
<body>
日 期: <input type="text" id="date"/><br /><br />
用户名: <input type="text" id="admin"/><br /><br />
<div><input type="button" value="提交" id="btn"/></div>
<script language="javascript">
$(function(){
$("#btn").click( function () {
var date = $("#date").attr("value");
var reg = /^\d{4}\/[0]\d|[1][012]\/[0-2]\d|[3][0-1]$/g;
var reg2 = /^\d{4}/
if(reg.test(date) == false)//验证日期
{
alert("日期格式不正确,应该为yyyy/mm/dd");
}
else
{
alert(date.match(reg2));
}
});
})
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>日期用化名</title>
<script type="text/javascript" language="javascript" src="jquery.js"></script>
</head>
<body>
日 期: <input type="text" id="date"/><br /><br />
用户名: <input type="text" id="admin"/><br /><br />
<div><input type="button" value="提交" id="btn"/></div>
<script language="javascript">
$(function(){
$("#btn").click( function () {
var date = $("#date").attr("value");
var reg = /^\d{4}\/[0]\d|[1][012]\/[0-2]\d|[3][0-1]$/g;
var reg2 = /^\d{4}/
if(reg.test(date) == false)//验证日期
{
alert("日期格式不正确,应该为yyyy/mm/dd");
}
else
{
alert(date.match(reg2));
}
});
})
</script>
</body>
</html>