2

I am getting validation message on chrome if i select date >12

I have used localised datepicker with syntax

var abc = $("#culture").val();
        $.datepicker.setDefaults($.datepicker.regional[""]);

        $("#dob").datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: 'dd-mm-yy',
            yearRange: '-90:-15',
            defaultDate: '-90y'

        });
        $.datepicker.setDefaults($.datepicker.regional[abc]);


another problem : I am getting date in undesirable format at controller, that is why my database holds wrong entry(date in place of month and month in place of date)..suggest me the possible solution...

What relevant i found is LINK 1

Link2

Link 3

But none of the above works...

4 Answers 4

0

First of all, if you are using MVC, and you show some date from the server, you should display the date like:

@Html.TextBox("InsertDate", Model.InsertDate.ToString("dd/MM/yyyy"), new { @class = "date" })

And in your jquery just :

$('.date').datepicker({ dateFormat: "dd/mm/yy" }).val();

The ModelState.IsValid will break if your culture is en-US. A dirty hack for this is something like this:

var insertDateStr = ModelState.Values.ToList()[3].Value.AttemptedValue;
var insertDate = DateTime.ParseExact(insertDateStr, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None);

And finally for the red on chrome just add background color white in the style of textbox.

@Html.TextBox("InsertDate", Model.InsertDate.ToString("dd/MM/yyyy"), new { @class = "date", @style = "background-color: white;" })

It is very dirty but it works..

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

Comments

0

The solution is not so simple, you should end up overriding the jquery validation rule. I recommend using the Globalize plugin as described in here MVC 3 jQuery Validation/globalizing of number/decimal field

Comments

0

Try this:

$("#dob").datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat: 'mm-dd-yy',
    yearRange: '-90:-15',
    defaultDate: '-90y'
});  

var abc = $("#culture").val();  

$("#dob").datepicker("setDate", new Date(abc));

$("#dob").datepicker({
    changeMonth: true,
    changeYear:  true,
    dateFormat:  'dd-mm-yy',
    yearRange:   '-90:-15',
    defaultDate: '-90y'
});

Otherwise you can get the date as a sting and then converted in to your required format.

If you're running with jQuery you can use the datepicker UI library's parseDate function to convert your string to a date:

var d = $.datepicker.parseDate("DD, MM dd, yy",  "Sunday, February 28, 2010");

And then follow it up with the formatDate method to get it to the string format you want:

var datestrInNewFormat = $.datepicker.formatDate( "mm/dd/yy", d);

If you're not running with jQuery of course it's probably not the best plan given you'd need jQuery core as well as the datepicker UI module... best to go with the suggestion from Segfault above to use date.js.

Comments

0

Use <globalization culture="en-GB"/> in web.config if you expect ASP.Net to acknowledge date in "dd-MM-yyyy" format.

reference

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.