日期验证 [英] Validation for Date

查看:120
本文介绍了日期验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,在此我必须验证格式如mm / dd / yyyy的一个日期字段。我在网上搜索,但我没有得到正确的。请通过提供新功能或通过更正我的代码来帮助我。我的代码如下所示。我在onblur事件中调用了这个JS函数。

  function isValidDate(){

var re = new RegExp('^(0 [1-9] | [12] [0-9] | 3 [01])/(0 [1-9] | 1 [012])/ (19 | 20)dd $');

if(form1.txtDateOfOccurance.value!=''&&form !txtDateOfOccurance.value.match(re)){
alert(Invalid date format:+ form1 .txtDateOfOccurance.value);
form1.txtDateOfOccurance.value =;
form1.txtDateOfOccurance.focus();
isEnable();
返回false;
}

}



感谢提前..

解决方案

我只是尝试将字符串解析为Date对象,并检查结果(假设只有需要知道是否有效的日期):

  var myDate = Date.parse(form1.txtDateOfOccurance.value); 
if(isNaN(myDate)){
//它不是一个真实的日期
}


I have one web application, in this i have to validate one date field of format like mm/dd/yyyy. I searched in the net but i didn't get the proper one. Please help me by providing the new function or by correcting on my code. My code is shown below.. I had called this JS function at onblur event..

function isValidDate() {

var re = new RegExp('^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/(19|20)dd$');

if (form1.txtDateOfOccurance.value != '' && !form1.txtDateOfOccurance.value.match(re)) {
    alert("Invalid date format: " + form1.txtDateOfOccurance.value);
    form1.txtDateOfOccurance.value = "";
    form1.txtDateOfOccurance.focus();
    isEnable();
    return false;
}

}

Thanks in advance..

解决方案

I'd just try parsing the string as a Date object and check the result (assuming you only need to know if it's a valid date or not):

var myDate = Date.parse(form1.txtDateOfOccurance.value);
if(isNaN(myDate)){ 
    // it's not a real date
}

这篇关于日期验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆