0

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?

1 Answer 1

1

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"

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for responding , its client requirement. They have this scripts VB script they want to migrate to Java script. Coming to Error...Java script is firing, everything is fine except displaying of the error message. The custom validator is expecting input as true or false. script is also throwing either true or false....but args is not accepted by custom control
stackoverflow.com/questions/15249848/… Are you sure it's not args.IsValid=true; ?
Thanks its worked for its args.IsValid=true instead of args.valid=true...Thanks for your help.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.