0

How do you implement the jQuery UI Datepicker validation when working in ASP.NET web form? The base for this validation is from this site: http://keith-wood.name/uiDatepickerValidation.html The problem I have is with the rule. Example:

$('#validateForm').validate({ 
errorPlacement: $.datepicker.errorPlacement, 
rules: { 
    txtDatepicker: { 
        required: true, 
        dpDate: true 
    }, 
}, 
messages: { 
    txtDatepicker: 'Please enter a valid date (yyyy-mm-dd)', 
}});

The "txtDatepicker" is the ASP:TextBox ID which changes when the page is rendered. How do I specify this in the above rule syntax? I tried to use property name="txtDatepicker" but it didn't work.

3 Answers 3

1
<asp:TextBox runat="server" Id="txtDatepicker" ClientIDMode="Static" />

Just change the ClientID Mode, then the ID on the client will always be txtDatepicker.

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

2 Comments

Mason, when I added the ClientIDMode="Static" now I am getting error "unable to get property 'settings' of the undefined or null reference. Thank you for the suggestion.
Check your JavaScript from the client side and make sure it matches what you're expecting.
0

Could do it like

$('#validateForm').validate({ 
errorPlacement: $.datepicker.errorPlacement, 
rules: { 
    <%=txtDatepicker.ClientID %>: { 
        required: true, 
        dpDate: true 
    }, 
}, 
messages: { 
    <%=txtDatepicker.ClientID %>: 'Please enter a valid date (yyyy-mm-dd)', 
}});

1 Comment

Mohamed, Your syntax does not work either. Visual studio shows squiggly red line under the colon which is right after the <%=txtDatePicker.ClientID %> What I tried also is to enclose the <% %> into double quotes. This removed the squiggly line but makes no difference in how it works.
0

Here is what I found that works (but for the life of me I don't understand why and how).

I have changed the rule as following:

$('#validateForm').validate({ 
 errorPlacement: $.datepicker.errorPlacement, 
rules: { 
  anystringorname: { 
  }, 
 }, 
 messages: { 
 }});

And then I add the class dpDate to my asp:textbox and everything works. That is, if I type in some bogus number (e.g. 12344) and click on Submit the focus is set to the asp:textbox and datepicker calendar appears. And if I type the valid date, the Submit works. So everything works but note that the code above has a bogus name in the rules and no actual rules. Yet if I comment the validate() all together, no validation takes place. And when typing a bogus number the Submit does not stop. If anybody see what I don't see, please let me know.

Comments

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.