Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Datepicker Will not post with UK date string

Apologies for if the question is obvious but I can't figure out why it is suddenly not working. I have a jquery datepicker that has been working fine as long as I can remember, but all of a sudden, when I try to submit the form that the datepicker is on the datepicker reappears as though the date I am submitting is invalid. I have already set my date to uk style using the following line of code:

    <script type="text/javascript">
    $(document).ready(function () {
        $('.date').datepicker({ dateFormat: "dd/mm/yy", minDate: 0 });

    }); 
    </script>

And I setup the datepicker in my view like this:

@Html.TextBox("DateDue", "", new { @class = "date" })

I think that something is requiring the datestring selected to be in US format (as it will submit a valid us date (mm/dd/yyyy) but not the UK format, even though it has been set in the javascript.

Am I missing something obvious? Could anyone tell me what I'm doing wrong here?

Am not sure if it is necessary but the form that the datepicker is on is created like this:

@using (
    Html.BeginForm(
        Html.BeginForm(
            "Create",
            "Task",
            FormMethod.Post,
            new { id = "taskCreateForm" }
        )
    )
)

PS. The datestring actually works and posts without a problem in Firefox but not in Chrome

Any help appreciated

EDIT:

The problem goes away when I disable the unobtrusive js validation:

<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"
        type="text/javascript"></script>

I had a feeling it was something to do with this and tried remoing the [Required] and [DataType(DataType.Date)] from the model annotation but this didn't do the trick.

Not sure how to leave the unobtrusive js in as well as have the uk date string working ok, but am more and more turning to write my own javascript validation for forms than to even bother with the built in stuff, which never seems to work for anything more complicated than a 'required' validation check..

like image 423
DevDave Avatar asked Nov 15 '11 15:11

DevDave


2 Answers

a google search gave a lot of results relating to localizing/globalizing asp.net mvc 3 validation.

this article in particular might be on to something:

http://www.hanselman.com/blog/GlobalizationInternationalizationAndLocalizationInASPNETMVC3JavaScriptAndJQueryPart1.aspx

scroll down to: "Globalized jQuery Unobtrusive Validation"

the answer to this question might help as well: Localizing jquery validation with asp.net mvc 3

jQuery allows you to override the different validation functions like so:

 $.validator.methods.number = function (value, element) {
     return !isNaN($.parseFloat(value));
 }

you could do something like this for the date validator, and run it against a regEx for a valid UK date.

like image 88
Patricia Avatar answered Sep 21 '22 21:09

Patricia


You can use the dateFormat, so in your javascript file (after the page is loaded etc.) you can place:

$('.datepicker').datepicker({dateFormat:'yy-mm-dd'});
like image 29
Tessmore Avatar answered Sep 23 '22 21:09

Tessmore