利用javascript验证输入框中的值是否为日期格式[网上找的]
1、判断是否为年月日时间格式
<script>
//去除字符串首尾空格
String.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
//验证是否为日期
function validator(){
if(isDate(document.all.demo1.value.trim())==false){
document.all.demo1.select();
return false;
}
}
/**
判断输入框中输入的日期格式是否为 yyyy-mm-dd 或yyyy-m-d
*/
function isDate(dateString){
if(dateString.trim()=="")return true;
//年月日正则表达式
var r=dateString.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null){
alert("请输入格式正确的日期\n\r日期格式:yyyy-mm-dd\n\r例 如:2008-08-08\n\r");
return false;
}
var d=new Date(r[1],r[3]-1,r[4]);
var num = (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
if(num==0){
alert("请输入格式正确的日期\n\r日期格式:yyyy-mm-dd\n\r例 如:2008-08-08\n\r");
}
return (num!=0);
}
</script>
测试是否为年月日时间格式
<input type='text' name='demo1' >
<br>
<input type='button' name='demo2' value="确定" onclick="validator()" >
以上代码实现了验证年月日
2、判断是否为年月日时分秒时间格式
<script>
//去除字符串首尾空格
String.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
//验证是否为日期
function validator(){
if(isDate(document.all.demo1.value.trim())==false){
document.all.demo1.select();
return false;
}
}
/**
判断输入框中输入的日期格式是否为年月日时分秒 即 yyyy-mm-dd hh:mi:ss
*/
function isDate(dateString){
if(dateString.trim()=="")return true;
//年月日时分秒正则表达式
var r=dateString.match(/^(\d{1,4})\-(\d{1,2})\-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
if(r==null){
alert("请输入格式正确的日期\n\r日期格式:yyyy-mm-dd\n\r例 如:2008-08-08\n\r");
return false;
}
var d=new Date(r[1],r[2]-1,r[3],r[4],r[5],r[6]);
var num = (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[2]&&d.getDate()==r[3]&&d.getHours()==r[4]&&d.getMinutes()==r[5]&&d.getSeconds()==r[6]);
if(num==0){
alert("请输入格式正确的日期\n\r日期格式:yyyy-mm-dd\n\r例 如:2008-08-08\n\r");
}
return (num!=0);
}
</script>
测试是否为年月日,时分秒的格式
<input type='text' name='demo1' >
<br>
<input type='button' name='demo2' value="确定" onclick="validator()" >
function DateFunction(oDateTime)
{
this.MinYear = 1970; //最大年份
this.MaxYear = 2030; //最小年份
this.DateFormat = "<F>"; //要输出时间的格式
//<yy> <yyyy>代表年, <m> <mm>代表月, <d> <dd>代表天<h> <hh>代表小时<n> <nn>代表分<s> <ss>代表秒
//for example:
// <yyyy>-<mm>-<dd> : 2002-04-01
// <yy>.<m>.<d> : 02.4.1
// <m>/<d>/<yy> : 4/1/02
// <dd>/<mm>/<yyyy> : 01/04/2002
// <hh>:<nn>:<ss> : 01:01:04
// <F>: 2002-04-01 01:01:04
// <S>: 2002-04-01
// <t>: 01:01:04
// <T>: 返回时间戳
this.GetDateTime=function(oDateTime) {//按照指定格式返回时间
return this.SetDateFormat(oDateTime);
}
this.Now=function(){//返回当前时间
var date=new Date()
return this.SetDateFormat(date);
}
this.SplitDate = function(oDateTime){//拆分时间,返回一个按照年月日时分秒排列的数组
var z1, z2, z3, z4, z5
if (isNaN(Date.parse(oDateTime)))
{
if (oDateTime.indexOf("-")!=-1)
{
z1 = oDateTime.indexOf("-");
z2 = oDateTime.indexOf("-", z1+1);
}
else if(oDateTime.indexOf("/")!=-1)
{
z1 = oDateTime.indexOf("/");
z2 = oDateTime.indexOf("/", z1+1);
}
szYear = Number(oDateTime.slice(0, z1));
szMonth = Number(oDateTime.slice(z1+1,z2)) -1 ;
if (oDateTime.indexOf(":")!=-1)
{
z3 = oDateTime.indexOf(" ", z2+1);
z4 = oDateTime.indexOf(":", z3+1);
z5 = oDateTime.indexOf(":", z4+1);
szDate = Number(oDateTime.slice(z2+1,z3)) ;
szHour = Number(oDateTime.slice(z3+1,z4));
szMinute = Number(oDateTime.slice(z4+1,z5));
szSecond = Number(oDateTime.slice(z5+1));
}
else
{
szDate = Number(oDateTime.slice(z2+1)) ;
szHour = Number(0);
szMinute = Number(0);
szSecond = Number(0);
}
}
else
{
szYear = oDateTime.getYear();
szMonth =oDateTime.getMonth();
szDate = oDateTime.getDate();
szHour = oDateTime.getHours() ;
szMinute =oDateTime.getMinutes() ;
szSecond = oDateTime.getSeconds();
}
var szTimeStamp=new Date(szYear,szMonth,szDate,szHour,szMinute,szSecond).valueOf();
var DateArray = new Array(szYear,szMonth,szDate,szHour,szMinute,szSecond,szTimeStamp);
return DateArray;
}
//---------------------------------------------------------------------
this.GetFormatYear = function(theYear){//把年份格式化成4位
var tmpYear = parseInt(theYear,10);
if (tmpYear < 100){
tmpYear += 1900;
if (tmpYear < 1940){
tmpYear += 100;
}
}
if (tmpYear < this.MinYear){
tmpYear = this.MinYear;
}
if (tmpYear > this.MaxYear){
tmpYear = this.MaxYear;
}
return(tmpYear);
}
//---------------------------------------------------------------------
this.GetMonthDays = function(theYear, theMonth){ //获得当年当月最大天数
var theDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var theMonthDay = 0, tmpYear = this.GetFormatYear(theYear);
theMonthDay = theDays[theMonth];
if (theMonth == 1){ //theMonth is February
if (((tmpYear % 4 == 0) && (tmpYear % 100 != 0)) || (tmpYear % 400 == 0)){
theMonthDay++;
}
}
return(theMonthDay);
}
//----------------------------------------------------------------------
this.SetDateFormat = function(oDateTime){//按照dateformat格式化时间
var theDate = this.DateFormat;
var DateArray = new Array();
DateArray = this.SplitDate(oDateTime);
var tmpYear = this.GetFormatYear(DateArray[0]);
var tmpMonth = DateArray[1];
var tmpDay = DateArray[2];
var tmpHour = DateArray[3];
var tmpMinute = DateArray[4];
var tmpSecond = DateArray[5];
if (tmpMonth < 0){
tmpMonth = 0;
}
if (tmpMonth > 11){
tmpMonth = 11;
}
if (tmpDay < 1){
tmpDay = 1;
}else{
tmpDay = this.GetMonthDays(tmpYear, tmpMonth);
if (DateArray[2] < tmpDay){
tmpDay = DateArray[2];
}
}
if (theDate.indexOf("<T>")!=-1)
{
theDate = new Date(tmpYear, tmpMonth,tmpDay,tmpHour,tmpMinute,tmpSecond).valueOf();
}
else if (theDate.indexOf("<t>")!=-1)
{
theDate = tmpHour.toString()+":"+tmpMinute.toString()+":"+tmpSecond.toString();
}
else if (theDate.indexOf("<F>")!=-1)
{
theDate = tmpYear.toString()+"-"+(tmpMonth + 1).toString()+"-"+tmpDay.toString()+" "+tmpHour.toString()+":"+tmpMinute.toString()+":"+tmpSecond.toString();
}
else if (theDate.indexOf("<S>")!=-1)
{
theDate = tmpYear.toString()+"-"+(tmpMonth + 1).toString()+"-"+tmpDay.toString();
}
else
{
theDate = theDate.replace(/<yyyy>/g, tmpYear.toString());
theDate = theDate.replace(/<yy>/g, tmpYear.toString().substr(2,2));
if (DateArray[1] < 9){
theDate = theDate.replace(/<mm>/g, "0" + (tmpMonth + 1).toString());
}else{
theDate = theDate.replace(/<mm>/g, (tmpMonth + 1).toString());
}
theDate = theDate.replace(/<m>/g, (tmpMonth + 1).toString());
if (DateArray[2] < 10){
theDate = theDate.replace(/<dd>/g, "0" + tmpDay.toString());
}else{
theDate = theDate.replace(/<dd>/g, tmpDay.toString());
}
theDate = theDate.replace(/<d>/g, tmpDay.toString());
if (DateArray[3] < 10){
theDate = theDate.replace(/<hh>/g, "0" + tmpHour.toString());
}else{
theDate = theDate.replace(/<hh>/g, tmpHour.toString());
}
theDate = theDate.replace(/<h>/g, tmpHour.toString());
if (DateArray[4] < 10){
theDate = theDate.replace(/<nn>/g, "0" + tmpMinute.toString());
}else{
theDate = theDate.replace(/<nn>/g, tmpMinute.toString());
}
theDate = theDate.replace(/<n>/g, tmpMinute.toString());
if (DateArray[5] < 10){
theDate = theDate.replace(/<ss>/g, "0" + tmpSecond.toString());
}else{
theDate = theDate.replace(/<ss>/g, tmpSecond.toString());
}
theDate = theDate.replace(/<s>/g, tmpSecond.toString());
}
return(theDate);
}