I am using Ajax.Begin Form in my MVC 3 + Razor application
using (Ajax.BeginForm("ActionName", "ControllerName", new AjaxOptions { OnBegin = "ValidateDateFunction('" + @abc.xyz + "')", HttpMethod = "POST", UpdateTargetId = "savebutton" }))
{
<input type="submit" value="Save" />
}
Below is how my onBegin method looks like. I am passing a value to this method, I am able to get a proper alert.
function ValidateDateFunction(id) {
alert(id);
if(some-ConditionUsing-formId)
{
return false;
}
return true;
}
Now using this I wanted that if my condition fails then action should not be called. But here in my case in both condition my action is called.
Please help on this.
Below is my actual validate method
function ValidateDateFunction(fId) {
var first = document.getElementById("startDate" + fId);
var second = document.getElementById("endDate" + fId);
if (first.value == "" && second.value != "") {
alert("Please select both dates");
return false;
}
else if (first.value != "" && second.value == "") {
alert("Please select both dates");
return false;
}
var startDateVal = new Date(first.value);
var endDateVal = new Date(second.value);
if (startDateVal.getTime() > endDateVal.getTime()) {
alert("Error ! The start date is after the end date!");
return false;
}
alert('should not reach here');
return true;
}