I have some Jquery that is doing validation. The validation works currently on the fly because its in an anonymous function that just runs.
When the user clicks the save button and the validation has failed I want the page not to post back, and not to reload (so the validation messages stay up).
Currently the page is posting back even though I'm doing e.preventDefault() and I'm loosing the validation messages. Any idea on how I can prevent the page from posting back?
here is the code:
<script type="text/javascript">
$(function () {
$(".dateTimePickerField").kendoDateTimePicker({
min: new Date(1950, 0, 1),
max: new Date(2049, 11, 31),
value: new Date(2000, 10, 1),
interval: 15
});
var validator = $(".dateTimePickerField").kendoValidator({
rules: {
dateValidation: function (e) {
var currentDate = Date.parse($(e).val());
if (!currentDate) {
return false;
}
return true;
}
},
messages: {
//Define your custom validation massages
required: "datetime required",
dateValidation: "Invalid datetime"
}
}).data("kendoValidator");
$('#bttnSaveGuru').click(function (e) {
if (!validator.validate()) {
e.preventDefault();
e.stopPropagation();
}
});
});
</script>
<button runat="server" id="bttnSaveGuru" class="saveButton" type="button" clientidmode="Static">Save</button>
<tr>
<td runat="server" id="tdDate"></td>
<td runat="server" id="tdDescription"></td>
<td runat="server" id="tdLocation"></td>
<td><input type="text" runat="server" id="txtDate" class="dateTimePickerField" style="width:180px;margin-right:55px;" required="required"/></td>
<td><input type="text" runat="server" id="txtDateEnd" class="dateTimePickerField" style="width:180px;margin-right:55px;" required="required"/></td>
</tr>
A solution that has been suggested to me is to run the validation in the onclick property of the button, like this
<button runat="server" id="bttnSaveGuru" class="saveButton" type="button" clientidmode="Static" onclick="if(!checkValidation()){return false;}">Save</button>
This does in fact prevent the page from posting back, but it requires inline code on the button and I would rather prevent the page from posting back in the anonymous function if I can.