1

I'm building an ASP.NET MVC 4 website. I have a date field in one of my model classes which is formatted as "dd.mm.yyyy". In my edit or create views, if I try to enter a date like "05.12.1975", server side validation works without any problems but client side Jquery validation fails and says that "The field Birth Date must be a date." But if I change the value to "05/12/1975" both of the validations work. Problem is about "." character in date field. How can I allow "." in client side validation?

Thanks.

Date field in my C# Class file:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Birth Date")]
public virtual DateTime BirthDate { get; set; }

1 Answer 1

2

When using client side validation for dates, you have to override the jQuery validation for date.

<script type="text/javascript">
    $(function () {
        $.validator.methods.date = function (value, element) {
            return this.optional(element) || Globalize.parseDate(value, "dd.MM.yyyy") !== null;
        }
    });
</script>

You have to reference the Globalize library and the appropriate culture in your HTML head. You can download it from https://github.com/jquery/globalize .

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.