有人可以帮助我将这些功能中的日期格式更改为YYYY / MM / DD,并且如果这些检查失败,还可以帮助我阻止默认表单提交吗?我的数据库是YYYY / MM / DD格式,我按日期查询,所以这很重要。谢谢你的帮助
function ValidateForm(ctrl){
var stdate = document.getElementById("start");
var endate = document.getElementById("end");
//Validate the format of the start date
if(isValidDate(stdate.value)==false){
return false;
}
//Validate the format of the end date
if(isValidDate(endate.value)==false){
return false;
}
//Validate end date to find out if it is prior to start date
if(checkEnteredDates(stdate.value,endate.value)==false){
return false;
}
//Set the values of the hidden variables
FROMDATE.value= stdate.value;
TODATE.value= endate.value;
return true;
}
//--------------------------------------------------------------------------
//This function verifies if the start date is prior to end date.
//--------------------------------------------------------------------------
function checkEnteredDates(stdateval,endateval){
//seperate the year,month and day for the first date
var stryear1 = stdateval.substring(6);
var strmth1 = stdateval.substring(0,2);
var strday1 = stdateval.substring(5,3);
var date1 = new Date(stryear1 ,strmth1 ,strday1);
//seperate the year,month and day for the second date
var stryear2 = endateval.substring(6);
var strmth2 = endateval.substring(0,2);
var strday2 = endateval.substring(5,3);
var date2 = new Date(stryear2 ,strmth2 ,strday2 );
var datediffval = (date2 - date1 )/864e5;
if(datediffval <= 0){
alert("Start date must be prior to end date");
return false;
}
return true;
}
//--------------------------------------------------------------------------
//This function validates the date for MM/DD/YYYY format.
//--------------------------------------------------------------------------
function isValidDate(dateStr) {
// Checks for the following valid date formats:
// MM/DD/YYYY
// Also separates date into month, day, and year variables
var datePat = /^(\d{2,2})(\/)(\d{2,2})\2(\d{4}|\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date must be in MM/DD/YYYY format")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
}
}
return true; // date is valid
}
答案 0 :(得分:0)
mplungjan提供了一些日期验证程序的链接,这是我喜欢的那个(也许我有偏见......):
根据您的格式修改它:
function isValidDate(s) {
var bits = s.split(/\D+/);
var d = new Date(bits[0], --bits[1], bits[2]);
return d && (d.getMonth() + 1) == bits[1] && d.getDate() == Number(bits[2]) || NaN;
}
因此,您的表单可以执行以下操作:
<form onsubmit="return isValidDate(this.start.value) && isValidDate(this.end.value)" ...>
虽然您可能希望验证比这更复杂。
所以我有一个开始日期和结束日期,我需要验证一些事项,1。)两个日期的格式为YYYY / MM / DD 2.)结束日期不在开始日期之前3.)他们已经在每个月输入正确的天数,占闰年的闰年,如果其中任何一个不正确,请不要提交表格
嗯,你应该能够从提供的内容中做到这一点。尝试:
function validateForm(form) {
var start = form.start.value;
var end = form.end.value;
var startDate = parseYMD(start);
var endDate = parseYMD(end);
return startDate < endDate && isValidDate(start) && isValidDate(end);
}
使用助手:
function parseYMD(s) {
var b = s.split(/\D+/);
return new Date(b[0], --b[1], b[2]);
}
并使用:
调用它<form onsubmit="return validateForm(this)" ...>