I am using jEditable plugin with the datepicker but I need validation for the date before submit it to the database. So i edit the jEditable-datepicker.js to something like this:
submit: function (settings, original) {
var form = this;
var tid = $(original).attr('id');
var input = $(original).find('input');
var newDate = input.val();
alert('id' + tid);
alert('newdate' + newDate);
$.ajax({
async: false,
url: '/Stock/CompareDate',
type: 'GET',
data: { id: tid, inputDate: newDate },
success: function (result) {
alert(result);
if (result < 0) {
alert("Expiry dare cannot be earlier than storage date");
return false;
}
else {
alert('else');
return true;
}
}
});
},
/* attach jquery.ui.datepicker to the input element */
plugin: function (settings, original) {
var form = this,
input = form.find("input");
// Don't cancel inline editing onblur to allow clicking datepicker
settings.onblur = 'nothing';
datepicker = {
dateFormat: 'D, dd M yy'
};
input.datepicker(datepicker);
}
I get the alert in the if statement, which means the validation method works, but the invalid value still get submitted to the database. I dont know why the return false dint stop the submit action.. I had tried to solve this problem for a long time, yet this is the time i get closest to the solution. I just dint know how can i stop submission in the if statement…
Do really need help here… Thanks….
(UPDATE: add in html)
My date field is in a table:
<td id="sdat@(item.FoodID)" class="storagedatepicker">
@String.Format("{0:ddd, d MMM yyyy}", item.StorageDate)
</td>
<td id="edat@(item.FoodID)" class="expirydatepicker">
@String.Format("{0:ddd, d MMM yyyy}", item.ExpiryDate)
The call to jeditable is like this:
$('.expirydatepicker').editable('@(Url.Action("Edit", "Stock"))',
{
type: 'datepicker',
indicator: 'saving...',
event: 'dblclick',
tooltip: 'Double click to edit...',
submit: '<img src="../../Content/Add_in_Images/ok.png" alt="ok"/>',
cancel: '<img src="../../Content/Add_in_Images/cancel.png" alt="cancel"/>',
style: 'inherit'
});
Get it work by not trying to stop the submission, but replace the input value with the default value if the validation failed. Thanks