3

I made a date format with jQuery Validator, in this format: dd/mm/yyyy.

But I'm having a problem with this format in Google Chrome. In IE or Firefox, the date format is working well, but in Google Chrome, the format accept in maximum 12/12/yyyy. For example, the date 20/12/2014 in IE is ok, but in Chrome, the date is not accept.

The code:

$.validator.addMethod(
        "brDate",
        function(value, element) {
            return value.match(/^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/);
        },
        "The correct date format is dd/mm/yyyy."
    );
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
marcelps
  • 309
  • 2
  • 16
  • Welcome to international dates! Dont validate your dates using a regular expression, instead use date function. Look up date functions in Jquery and you'll find something – Toby Allen Apr 04 '14 at 20:01

2 Answers2

1

Most of the validators already have date validation methods.

Which one are you using?

http://**formvalidator.net**/#default-validators_dates
<!-- Validate date formatted yyyy-mm-dd -->
<input type="text" data-validation="date">

<!-- Validate date formatted dd/mm/yyyy -->
<input type="text" data-validation="date" data-validation-format="dd/mm/yyyy">

I'd suggest to use such validation.

If you want to write pattern you self, you can use this one:

^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$

MM/dd/yyyy with 100% leap years. Valid since year 1900. MM and DD could have 1 or 2 digits : M/d/yyyy or MM/d/yyyy or M/dd/yyyy

Taken from http://regexlib.com/REDetails.aspx?regexp_id=1071

Denis Obydennykh
  • 320
  • 2
  • 12
  • I tried using data-validation in inputs, but didn't work. For example, I digit 18/15/2014 (dd/mm/yyyy), and this date was registered in DB. It was wrong! But I solved the problem as say below. – marcelps Apr 04 '14 at 20:30
1

finally I solved the problem.

jQuery.validator.methods["date"] = function (value, element) { return true; } 

font: Unobtrusive validation in Chrome won't validate with dd/mm/yyyy

Community
  • 1
  • 1
marcelps
  • 309
  • 2
  • 16
  • 2
    For future viewers, note that this would accept any value as a date (decimals, letters, etc). It may be sufficient to do server-side parsing and so you simply want to allow the form to post, but just be aware of the implications. – DuncanMack Dec 19 '14 at 15:00
  • This is not really a solution. This just ignore date validation: always `return true` – Balde Feb 05 '19 at 16:21