Reputation: 23
I have aspnet application with custom validator using java script. I have to compare 2 dates in textboxes (txtbeginDate ,txtEndDate). I wrote my java script as
Java Script:
function DateCompareClient(oSrc, args)
{
var begindt = new Date(document.getElementById('txtBeginDate').value);
var endDt = new Date(document.getElementById('txtEndDate').value);
if (begindt < endDt) {
args.valid = true;
return;
}
args.valid = false;
return;
}
<asp:CustomValidator
ID="Customvalidator3" runat="server"
ControlToValidate="txtEndDate" ErrorMessage="End Date must be later than Begin Date"
EnableClientScript="true" ClientValidationFunction="DateCompareClient"
>*</asp:CustomValidator></td>
Now , script is runing fine, but its not displaying error message. if condition is false , it should display error , which is not happening?
Upvotes: 0
Views: 40
Reputation: 15041
What you are trying to do can be accomplished without any javascript, called the Compare Validator
<asp:CompareValidator id="compareStartAndEndDates"
ControlToValidate="txtEndDate"
ControlToCompare="txtBeginDate"
Operator="LessThan"
Text="End Date must be after Begin Date"
Type="Date"
runat="server"/>
I suspect that your validator is never actually firing. Stick an alert()
in your javascript to test that.
On your textboxes, you want to make sure they are set to AutoPostBack="true"
so that when the user moves away from that field, the validator kicks in. You might also need CausesValidation="true"
Upvotes: 1