日期验证 [英] Validation for Date
本文介绍了日期验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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屋!
查看全文