0

I'm following a tutorial from here

For some reason when I try to create a new user with a date it won't accept it unless the month is January between dates ranging from 1-12ish.

I'm pretty sure it's because of the ValidationMessageFor(in the User.cs) method which forces me to enter a date which month must be January and I don't know where to alter it.

Enrollment Error

jquery.validate

jquery.validate.unobtrusive

TykiMikk
  • 1,058
  • 3
  • 15
  • 31
  • What is the culture on the server (looks like its one that expects dates in the format `dd/MM/yyyy`)? –  Feb 18 '16 at 03:11
  • 1
    It should be mm/dd/yy but it won't accept 1/13/2016 OR 13/1/2016. It accepts both 12/01/2016 and 1/12/2015 which is confusing me – TykiMikk Feb 18 '16 at 03:14
  • Well what is the culture? –  Feb 18 '16 at 03:18
  • 1
    I don't understand what you mean by culture but it uses a Localhost Database and SQL server. There's more information here http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application – TykiMikk Feb 18 '16 at 03:26
  • What is the language on your machine? –  Feb 18 '16 at 03:29
  • 1
    C# is the language I'm coding in – TykiMikk Feb 18 '16 at 09:53
  • 1
    en.US Why does it matter? – TykiMikk Feb 18 '16 at 09:59
  • Because different cultures require different DateTime formats. Do you get the same error when you enter the full year `2/18/2016`? –  Feb 18 '16 at 10:02
  • 1
    I don't know why but it works when I'm at home using my own laptop. I'll try it tomorrow to see if it works on my work computer. – TykiMikk Feb 18 '16 at 10:09
  • 1
    Ok so it doesn't work at work. The system locale is English (New Zealand) – TykiMikk Feb 18 '16 at 22:01
  • `en.NZ` expects dates in the format `dd/MM/yyyy` so `2/18/2016` is invalid, and to represent the 18th of February, it needs to be `18/2/2016` in order to bind on the server. However, if you have client side validation (`jquery.validate.js` and `jquery.validate.unobtrusive.js`) then that 'valid' date will display a validation error because `jquery.validate` validates dates based on the `MM/dd/yyyy` format. If thats the case you need to reconfigure the validator. –  Feb 18 '16 at 22:06
  • 1
    I edited my question and included the jquery.validate and jquery.validate.unobtrusive.js files. Could you point to where I should be looking because I'm very new to this. – TykiMikk Feb 18 '16 at 22:19
  • (1) Are you saying you did not previously have those scripts included? And (2) are you wanting to to enter `2/18/2016` or `18/2/2016` to represent 18th Feb? –  Feb 18 '16 at 22:23
  • 1
    (1) No I just uploaded the 2 files from my solution folder. (2) I want 18/2/2016 to represent 18th Feb. dd/mm/yy! Thanks in advance – TykiMikk Feb 18 '16 at 22:50
  • OK, First comment out those 2 scripts for testing and confirm that `18/2/2016` is now working correctly (`ModelState` is valid) –  Feb 18 '16 at 22:52
  • 1
    What do you mean by comment out those 2 scripts?? If you mean paste the code here it's too big . – TykiMikk Feb 18 '16 at 23:05
  • If you added then to the view, then just temporary comment them out so client side validation is not triggered. You need to first confirm that posting a valid date (i.e. `/18/2/2016`) is working and `ModelState` is valid. –  Feb 18 '16 at 23:07
  • 1
    I commented out everything in both jquery files but it still doesn't work. I didn't add those two jquery files to the view. They were already in there! I just uploaded them for you to see. – TykiMikk Feb 18 '16 at 23:11
  • 1
    What do you mean _I commented out everything in both jquery files_?? I mean just to comment out the lines `` etc. in your view. And of course it works. If not then there is something else in your code which you have not shown us. But based on all your previous comments it clear you have no understanding of MVC, so I suggest you spend some time learning. –  Feb 18 '16 at 23:16
  • 1
    Yes I commented it out and now it works! Thanks! Yes I'm very new to MVC so I'm googling around but there isn't much of a complete course on it. Any suggestions where I should look at apart from googling? – TykiMikk Feb 18 '16 at 23:26

1 Answers1

-1

Add code into script

$.validator.addMethod('date', function (value, element) {
  if (this.optional(element)) {
    return true;
  }
  var valid = true;
  try {
    $.datepicker.parseDate('dd/mm/yy', value);
  }
  catch (err) {
    valid = false;
  }
  return valid;
});
$('#dt1').datepicker({ dateFormat: 'dd/mm/yy' });
TykiMikk
  • 1,058
  • 3
  • 15
  • 31
  • No!! Never do that! It means you will never get client validation for anything. I meant to temporarily comment out the scripts in the **view** for testing. The `jquery.validate.js` file validates dates based on the `MM/dd/yyyy` so if you want client side validation for dates in `dd/MM/yyyy` format then you need to reconfigure the validator orr use the `jquery.globalize.js` plugin - refer [this answer](http://stackoverflow.com/questions/27285458/jquery-ui-datepicker-and-mvc-view-model-type-datetime/27286969#27286969) for more details –  Feb 18 '16 at 23:32
  • 1
    Sorry to bother you again so where abouts to I paste the code you provided in the question you posted? Anywhere in scripts\jquery.validate.js? – TykiMikk Feb 18 '16 at 23:46
  • 1
    That code was associated with using the jquery-ui datepicker plugin (and would not work for a normal textbox). I suggest you use the globalize plugin as suggested in the link. And you never modify the `jquery.validate.js` file - you write you own script(s). –  Feb 18 '16 at 23:54