<table align=center>
<tr> <th>Check/UnCheck</th>
<th>MYear</th>
<th>P Date</th>
<th>Q Date</th>
<th>B Date</th>
</tr>
<%for(int i=0 ; i<mVector.size();i++)
{
%>
<tr>
<td align="top">
<INPUT type="checkbox" class="chkValues" id="ckbCheckAll" name="chkBox" type="checkbox" data-row="row<%=i%>" value="" size="50">
</td>
<td align="left">
<input class="inputText row<%=i%>" type="text" name="mYear" id="mYear" value="<%=mVector.elementAt(i)%>">
</td>
<td>
<input class="date row<%=i%>" id="pDate<%=i%>" name="dob" type="text" value="<%=(oldValues.elementAt(0)==null?"":oldValues.elementAt(0))%>" />
</td>
<td>
<input class="date row<%=i%>" id="qDate<%=i%>" name="dob" type="text" value="<%=(oldValues.elementAt(1)==null?"":oldValues.elementAt(1))%>" />
</td>
<td>
<input class="date row<%=i%>" id="bDate<%=i%>" name="dob" type="text" value="<%=(oldValues.elementAt(2)==null?"":oldValues.elementAt(2))%>" />
</td>
</tr>
<%}%>
</table>
$(document).ready(function() {
$(".date").datepicker({dateFormat:'dd/mm/yy'});
$('.chkValues').on('click', function() {
//get values of dates
var array = [];
$row = $(this).data('row'); //row1, row2, row3 etc
$('input.' + $row).each(function() {
array.push($(this).val());
});
$('#btnSubmit').click(function(evt) {
evt.preventDefault();
$.ajax({
type: "POST",
url: "Test.jsp",
data: {array : array},
success: function(responseFromServer) {
$.each(responseFromServer, function(resultMessageKey,resultMessageValue) {
$('#content').html(resultMessageKey);
});
//getClearDateValues();
},
error: function() {
alert(" Ajax call Failed to Update Values into Database ");
}
});
});
});
});
in above code it is comparing with 0 how can i get previous date and compare with other clicked date .The above code is correct as it stands.
$(function() {
$('.chkValues').on('click', function() {
// First get the parent row.
var parent = $(this).closest('tr');
// Now find all the child <inpu> elements with class "date"
var last = 0;
$('.date', parent).each(function(i,e) {
var dt = new Date(e.value);
// Compare this input to the value of the last and if it is less than or equal display an error
if (dt.getTime() <= last) {
alert('Error: ' + e.id + ' has an invalid value');
}
last = dt.getTime();
});
});
});
[code]$(document).ready(function() {
$(".date").datepicker({dateFormat:'dd/mm/yy'});
$('.chkValues').on('click', function() {
//get values of dates
var array = [];
var parent = $(this).closest('tr');
// Now find all the child <inpu> elements with class "date"
var last = 0;
$('.date', parent).each(function(i,e) {
var dt = new Date(e.value);
console.log(dt.getTime());
if (dt.getTime() <= last) {
alert('Error: ' + e.id + ' has an invalid value');
}
last = dt.getTime();
});
$row = $(this).data('row'); //row1, row2, row3 etc
$('input.' + $row).each(function() {
array.push($(this).val());
});
$('#btnSubmit').click(function(evt) {
evt.preventDefault();
$.ajax({
type: "POST",
url: "Test.jsp",
data: {array : array},
success: function(responseFromServer) {
$.each(responseFromServer, function(resultMessageKey,resultMessageValue) {
$('#content').html(resultMessageKey);
});
},
error: function() {
alert(" Ajax call Failed to Update Values into Database ");
}
});
});
});
});
<tr>
<th>Check/UnCheck</th>
<th>MYear</th>
<th>P Date</th>
<th>Q Date</th>
<th>B Date</th>
</tr>
<tr>
<td align="top">
<input type="checkbox" class="chkValues" id="ckbCheckAll" name="chkBox" type="checkbox" data-row="1" value="" size="50">
</td>
<td align="left">
<input class="inputText row1" type="text" name="mYear" id="mYear" value="2020">
</td>
<td>
<input class="date row1" id="pDate1" name="dob" type="date" value="2020-01-01" />
</td>
<td>
<input class="date row1" id="qDate1" name="dob" type="date" value="2020-01-01" />
</td>
<td>
<input class="date row1" id="bDate1" name="dob" type="date" value="2020-01-01" />
</td>
</tr>
<tr>
<td align="top">
<input type="checkbox" class="chkValues" id="ckbCheckAll" name="chkBox" type="checkbox" data-row="2" value="" size="50">
</td>
<td align="left">
<input class="inputText row2" type="text" name="mYear" id="mYear" value="2020">
</td>
<td>
<input class="date row2" id="pDate2" name="dob" type="date" value="2020-02-01" />
</td>
<td>
<input class="date row2" id="qDate2" name="dob" type="date" value="2020-02-01" />
</td>
<td>
<input class="date row2" id="bDate2" name="dob" type="date" value="2020-02-01" />
</td>
</tr>
</table>
var dateParts = e.value.split('/');
var dt = new Date(dateParts[2], dateParts[0], dateParts[1]);
// Date is now in correct format
if (dt.getTime() <= last) {
alert('Error: ' + e.id + ' must be greater than ' + lastId);
}
...
lastId = e.id;
var valid = true;
if (dt.getTime() <= last) {
alert('Error: ' + (i+1) + lbl[i] + ' date must be greater than ' + i + lbl[i-1] + ' date');
valid = false;
}
...
if (valid) {
// do AJAX here
}
[..] one bug is coming when validation fails then also ajax request is executing please tell me the solution.Obviously. Cause you mixed the checkbox event with the submit event..
$(document).ready(function() {
$(".date").datepicker({dateFormat:'dd/mm/yy'});
$('.chkValues').on('click', function() {
$('#btnSubmit').click(function(evt) {
});
});
});
instead of$(document).ready(function() {
$(".date").datepicker({dateFormat:'dd/mm/yy'});
$('.chkValues').on('click', function() {
});
$('#btnSubmit').click(function(evt) {
});
});
To validate - are you validating the entire table or only a specific row?
This seems to be a duplicate / extension of your other question (https://www.experts-exchange.com/questions/29168950/in-grid-view-checkbox-is-Enable-while-particular-row-cell-is-clicked.html) where I asked you some questions that you have not answered.
Still don't have the answers but will try to answer your question here.
Firstly I cannot see the sense in the checkbox - you have not explained this functionality - I am assuming that (in this case) the user must click the checkbox when they have changed the dates (in the other question it seems the checkbox has a different function).
Let's go with the click checkbox to validate
(Assumes the table is not dynamically generated - if it is then the event below will not fire after the table has been dynamically re-rendered).
Open in new window