Important Update: Community URLs redirect issues are partially resolved. Learn More. .
2019-05-21 07:52 AM
Hello,
We have written a date validation code for a core application. It is working as expected in 6.6 but it is not working in 6.5. This is very weird.
Use case - two date fields, one of them should not be less than another. If it is, then it should not allow the record to be saved.
Issue facing - It is throwing the alert but the record still saves.
Thanks in advance
2019-05-21 08:00 AM
Swagatika, mind sharing the code, please? It is impossible to say the reasoning without the code itself.
2019-05-22 02:02 AM
Hi Ilya,
Below is the code:
<script type="text/javascript">
//Define date field ids
var startDate = 27957, startDateValue;
var endDate = 27958, endDateValue;
Sys.Application.add_load(function () {
// Injecting Validation Module to Save and Apply
$('#master_btnSave').attr('href', "#").removeAttr("onclick").unbind("click").click(function () { DateCheck('save'); return false; });
$('#master_btnSave > div > div > img').removeAttr('onclick').unbind("click");
$('#master_btnApply').attr('href', "#").removeAttr('onclick').unbind("click").click(function () { DateCheck('apply'); return false; });
$('#master_btnApply > div > div > img').removeAttr('onclick').unbind("click");
});
//Date validation module
function DateCheck(type) {
startDateValue = new Date(String($CM.getFieldValue(startDate, false)));
endDateValue = new Date(String($CM.getFieldValue(endDate, false)));
if (startDateValue && endDateValue) {
if (daydiff(parseDate(startDateValue), parseDate(endDateValue)) < 0) {
var msg = "Start Date Cannot be Less Than End Date";
var title = 'Warning';
WarningAlert(msg, '', title);
return false;
} else {
SaveApply(type)
}
} else {
SaveApply(type)
}
}
function SaveApply(type) {
if (type == 'save') {
ShowAnimationAndPostback('master$btnSave');
} else if (type == 'apply') {
ShowAnimationAndPostback('master$btnApply');
}
}
//Date Parsing module
function parseDate(str) {
str = str.getMonth() + 1 + '/' + str.getDate() + '/' + str.getYear();
var mdy = str.split('/')
return new Date(mdy[2], mdy[0] - 1, mdy[1]);
}
//Date Differential calculation module
function daydiff(first, second) {
return (second-first)/(1000*60*60*24)
}
</script>
2019-05-22 02:09 AM
Try this:
<script type = "text/javascript" >
//Define date field ids
var startDate = 27957,
startDateValue;
var endDate = 27958,
endDateValue;
Sys.Application.add_load(function() {
$('#master_btnSave').clone().attr('id', 'master_customBtnSave').insertBefore('#master_btnSave');
$('#master_btnSave').hide();
$('#master_customBtnSave').unbind('click').prop("onclick", null).click(function(){
DateCheck('save');
return false;
});
$('#master_btnApply').clone().attr('id', 'master_customBtnApply').insertBefore('#master_btnApply');
$('#master_btnApply').hide();
$('#master_customBtnApply').unbind('click').prop("onclick", null).click(function(){
DateCheck('apply');
return false;
});
});
//Date validation module
function DateCheck(type) {
startDateValue = new Date(String($CM.getFieldValue(startDate, false)));
endDateValue = new Date(String($CM.getFieldValue(endDate, false)));
if (startDateValue && endDateValue) {
if (daydiff(parseDate(startDateValue), parseDate(endDateValue)) < 0) {
var msg = "Start Date Cannot be Less Than End Date";
var title = 'Warning';
WarningAlert(msg, '', title);
return false;
} else {
SaveApply(type)
}
} else {
SaveApply(type)
}
}
function SaveApply(type) {
if (type == 'save') {
$('#master_btnSave').click();
} else if (type == 'apply') {
$('#master_btnApply').click();
}
}
//Date Parsing module
function parseDate(str) {
str = str.getMonth() + 1 + '/' + str.getDate() + '/' + str.getYear();
var mdy = str.split('/')
return new Date(mdy[2], mdy[0] - 1, mdy[1]);
}
//Date Differential calculation module
function daydiff(first, second) {
return (second - first) / (1000 * 60 * 60 * 24)
}
</script>
2019-05-22 02:12 AM
Kunwar/Swagatika,
Just a suggestion, prefer using $('#master_btnApply').click() over ShowAnimationAndPostback('master$btnApply'); and $('#master_btnSave').click() over ShowAnimationAndPostback('master$btnSave');
2019-05-23 01:30 AM
Hi Ilya,
Thanks for the code, it is working as expected. I wonder what's the difference in my code v/s yours?
Mine is working in 6.6 but not on 6.5
Your comments appreciated.
Thanks,
Kunwar Divyanshu
2019-05-23 01:40 AM
Kunwar, well you use pretty old constructions on button event overriding and saving functions.
Seems like 6.6 is more tolerant
2019-05-23 02:03 AM
Looks like so. Anyway, this works for me so would be using your code. Thanks for your help!
2019-05-23 02:04 AM
Anytime