1

I'm having a problem to validate the date field in my system (asp.net). I already standardized the fields in the class with annotations:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]

It's working quite good, showing me the fields like i want, but when i try to edit, the forms show me a error like this:

The field [field] must be a date.

So i can't save the editions because it's doesn't pass by the jquery validator.

I've standardized in c# annotations to dd/mm/yyy but the jquery is set to mm/dd/yyyy.

During my tests, i found (i guess) that one of this files is that one which makes the validation for the date field in the forms:

  • jquery.validate.js
  • jquery.validate.unobtrusive.js

I found this because when i remove this scripts from the page, the validation doesn't work, but i'm not sure also if this scripts call anothers.

And in the jquery.validate.js i found this functions and i tried to edit it in many ways, but without success:

    date: function( value, element ) {
                return this.optional(element) || !/Invalid|NaN/.test(new Date(value).toString());
},

    dateISO: function( value, element ) {
                return this.optional(element) || /^\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}$/.test(value);
},

I'm not sure that i'm modifying the right thing, so i need a hand here, please.

As i don't know what to modify, here's the link to jquery.validate.js.

https://github.com/KaioMartins/PersonalLibrary/blob/master/PersonalLibrary/Scripts/jquery.validate.js

KaioMartins
  • 115
  • 1
  • 9
  • `new Date(value)` is not a good idea (especially in a validation routine), parsing with the built–in Date constructor (or Date.parse) is strongly recommended against. – RobG Nov 28 '16 at 01:52
  • 1
    What do you suggest to do then? – KaioMartins Nov 28 '16 at 09:03
  • Write your own parser (simple if you only have one format to support), or use a library that has a parser (there are plenty of good ones available). But either way, you must know the format of the string you're trying to parse. Otherwise, you're just leaving it to chance. – RobG Nov 28 '16 at 09:13

1 Answers1

1

You can write your own parser function, as @RobG suggested, OR you can modify the original one, if you don't use it in your page. I use old versions of Globalize plugin and there was some functions to change the 'language' for validation plugin, as numbers with 1.234,56 format (instead standard 1,234.56 english format) and so on...

In your case, you can use:

$.validator.methods.date = function (value, element) { /* place your function here */ }

to override the default behavior.

And you can find reg expr for dd/MM/yyyy here: Regex to validate date format dd/mm/yyyy

EDIT: Where do you do this? Ok, in your own javascript code, just after load jquery.validate plugin. You can wait to onReady event, too.

<script src="jquery.validate.min.js"></script>
<script>
    $(function(){
        $.validator.methods.date = ...
    })
<script>
Community
  • 1
  • 1
Tistkle
  • 500
  • 6
  • 13