patternjavascriptMinor
Validating a date in a grid
Viewed 0 times
gridvalidatingdate
Problem
This function checks if the
I need some help in optimizing this block of code. It looks dirty right now.
```
function onAccrualEndDateChange(current) {
var endDate = $(current).val();
if (endDate != "") {
var actualEndDate = endDate;
if (actualEndDate != null) {
var tempEndDate = parseInt(actualEndDate.getMonth() + 1) + "/" + actualEndDate.getDate() + "/" + actualEndDate.getFullYear();
if (ValidateDate(tempEndDate)) {
var startDate = $(current).closest("tr").find("input[id$='StartDate']").val();
if (startDate != "") {
var actualStartDate = startDate;
if (actualStartDate != null) {
var tempStartDate = parseInt(actualStartDate.getMonth() + 1) + "/" + actualStartDate.getDate() + "/" + actualStartDate.getFullYear();
if (ValidateDate(tempStartDate)) {
if (actualStartDate.getDate() > actualEndDate.getDate() || actualStartDate.getMonth() > actualEndDate.getMonth() || actualStartDate.getFullYear() > actualEndDate.getFullYear()) {
$(current).closest("td").addClass("cell-error");
} else {
$(current).closest("td").removeClass("cell-error");
}
}
else {
$(current).closest("td").addClass("cell-error");
}
}
else {
$(current).closest("td").addClass("cell-error");
}
} else {
$(current).closest("tr").find("input[id$='StartDate']").closest("td").addClass("cell-error");
}
}
else {
$(current).closest("td").addClass("cell-error");
}
}
else {
$(current).closest("td").addClass("
EndDate of my grid is valid or not. If it is not valid, it turns the bgcolor of the cell to red color with cell-error class.I need some help in optimizing this block of code. It looks dirty right now.
```
function onAccrualEndDateChange(current) {
var endDate = $(current).val();
if (endDate != "") {
var actualEndDate = endDate;
if (actualEndDate != null) {
var tempEndDate = parseInt(actualEndDate.getMonth() + 1) + "/" + actualEndDate.getDate() + "/" + actualEndDate.getFullYear();
if (ValidateDate(tempEndDate)) {
var startDate = $(current).closest("tr").find("input[id$='StartDate']").val();
if (startDate != "") {
var actualStartDate = startDate;
if (actualStartDate != null) {
var tempStartDate = parseInt(actualStartDate.getMonth() + 1) + "/" + actualStartDate.getDate() + "/" + actualStartDate.getFullYear();
if (ValidateDate(tempStartDate)) {
if (actualStartDate.getDate() > actualEndDate.getDate() || actualStartDate.getMonth() > actualEndDate.getMonth() || actualStartDate.getFullYear() > actualEndDate.getFullYear()) {
$(current).closest("td").addClass("cell-error");
} else {
$(current).closest("td").removeClass("cell-error");
}
}
else {
$(current).closest("td").addClass("cell-error");
}
}
else {
$(current).closest("td").addClass("cell-error");
}
} else {
$(current).closest("tr").find("input[id$='StartDate']").closest("td").addClass("cell-error");
}
}
else {
$(current).closest("td").addClass("cell-error");
}
}
else {
$(current).closest("td").addClass("
Solution
This isn't "optimized", I just tried to make it more readable.
As I cant test it (really wish people would make jsbin examples to test with, buggered if Im going to do it) I have no idea if it works ;)
As I cant test it (really wish people would make jsbin examples to test with, buggered if Im going to do it) I have no idea if it works ;)
function onAccrualEndDateChange(current) {
var endDate = $(current).val();
if (endDate === "") {
$(current).closest("td").removeClass("cell-error");
return;
}
if (endDate === null) {
$(current).closest("td").addClass("cell-error");
return;
}
var actualEndDate = {
month: endDate.getMonth(),
date: endDate.getDate(),
fullYear: endDate.getFullYear()
};
var tempEndDate = parseInt(actualEndDate.month + 1, 10) + "/" + actualEndDate.date + "/" + actualEndDate.fullYear;
if (!ValidateDate(tempEndDate)) {
$(current).closest("td").addClass("cell-error");
return;
}
var startDate = $(current).closest("tr").find("input[id$='StartDate']").val();
if (startDate === "") {
$(current).closest("tr").find("input[id$='StartDate']").closest("td").addClass("cell-error");
return;
}
if (startDate === null) {
$(current).closest("td").addClass("cell-error");
return;
}
var actualStartDate = {
month: startDate.getMonth(),
date: startDate.getDate(),
fullYear: startDate.getFullYear()
};
var tempStartDate = parseInt(actualStartDate.month + 1, 10) + "/" + actualStartDate.date + "/" + actualStartDate.fullYear;
if (!ValidateDate(tempStartDate)) {
$(current).closest("td").addClass("cell-error");
return;
}
if (actualStartDate.date > actualEndDate.date || actualStartDate.month > actualEndDate.month || actualStartDate.fullYear > actualEndDate.fullYear) {
$(current).closest("td").addClass("cell-error");
} else {
$(current).closest("td").removeClass("cell-error");
}
}Code Snippets
function onAccrualEndDateChange(current) {
var endDate = $(current).val();
if (endDate === "") {
$(current).closest("td").removeClass("cell-error");
return;
}
if (endDate === null) {
$(current).closest("td").addClass("cell-error");
return;
}
var actualEndDate = {
month: endDate.getMonth(),
date: endDate.getDate(),
fullYear: endDate.getFullYear()
};
var tempEndDate = parseInt(actualEndDate.month + 1, 10) + "/" + actualEndDate.date + "/" + actualEndDate.fullYear;
if (!ValidateDate(tempEndDate)) {
$(current).closest("td").addClass("cell-error");
return;
}
var startDate = $(current).closest("tr").find("input[id$='StartDate']").val();
if (startDate === "") {
$(current).closest("tr").find("input[id$='StartDate']").closest("td").addClass("cell-error");
return;
}
if (startDate === null) {
$(current).closest("td").addClass("cell-error");
return;
}
var actualStartDate = {
month: startDate.getMonth(),
date: startDate.getDate(),
fullYear: startDate.getFullYear()
};
var tempStartDate = parseInt(actualStartDate.month + 1, 10) + "/" + actualStartDate.date + "/" + actualStartDate.fullYear;
if (!ValidateDate(tempStartDate)) {
$(current).closest("td").addClass("cell-error");
return;
}
if (actualStartDate.date > actualEndDate.date || actualStartDate.month > actualEndDate.month || actualStartDate.fullYear > actualEndDate.fullYear) {
$(current).closest("td").addClass("cell-error");
} else {
$(current).closest("td").removeClass("cell-error");
}
}Context
StackExchange Code Review Q#51990, answer score: 2
Revisions (0)
No revisions yet.