5

my view to display date from database

<div class="input-group">
    @Html.TextBoxFor(model => model.domain_renew_date, "{0:dd/MMM/yyyy}", new { @type = "datetime", @class = "form-control" })
    @Html.ValidationMessageFor(model => model.domain_renew_date, "", new { @class = "text-danger" })
</div>

my javascript for datepicker

 <script type="text/javascript">
    $(document).ready(function () {
        $('input[type="datetime"]').datepicker({
            dateFormat: "dd/MM/yy",
            changeMonth: true,
            changeYear: true,
            yearRange: "c-10:c+10"
        });
    });
</script>

model where the property is declared

[Display(Name = "Date of Renewal")]
[DataType(DataType.Date)]
[RequiredIf("domain_flag", "1", ErrorMessage = "Enter Renew Date")]
public DateTime? domain_renew_date { get; set; }

in web.config

<system.web>
  <globalization uiCulture="en" culture="en-GB" />
</system.web>

This is working perfectly fine in chrome and Firefox. But gives the following error in IE and Safari

The field Date of Renewal must be a date

What changes can be done in the code to make it work perfectly fine in all the browsers?

6
  • instead of $('input[type="datetime"]') use same class for all elements on which you want to show date-picker and then do like this $('.class-name').datepicker({. I hope this will work for all browers Commented Jul 6, 2017 at 5:08
  • Or take help from this:- stackoverflow.com/a/33591786/4248328 Commented Jul 6, 2017 at 5:11
  • its still giving a error Commented Jul 6, 2017 at 5:21
  • in both I m getting error in IE browser Commented Jul 6, 2017 at 5:28
  • check different solutions of this thread :- stackoverflow.com/a/29273831/4248328. Commented Jul 6, 2017 at 5:31

1 Answer 1

2

The error got solved by adding the following code

<script>
jQuery.validator.addMethod(
     'date',
     function (value, element, params) {
         if (this.optional(element)) return true;
         var ok = true;
         try {
             $.datepicker.parseDate('dd-M-yy',value);
         } catch (err) {
             ok = false;
         }
         return ok;
     },
      'Must enter date in dd-M-yy format.'
  );

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

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.