Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

input type="date" fails validation when using locale specific methods from localizations/ #2359

Closed
tyomitch opened this issue Nov 18, 2020 · 5 comments · Fixed by #2360
Closed
Labels
stale Used to mark stale issues

Comments

@tyomitch
Copy link
Contributor

https://jqueryvalidation.org/date-method/ says: This method is deprecated and will be removed in version 2.0.0.
Please don't use it, since it relies on the Date constructor, which behaves very differently across browsers and locales. Use dateISO instead or one of the locale specific methods (in localizations/ and additional-methods.js).

So we're using localization/methods_pt.js, and observing that it causes validation of date inputs to always fail, because they are unconditionally validated using the deprecated method, and not its suggested replacements.

Environment

  • jquery@3.5.1
  • jquery-validation@1.19.2
  • jquery-validation-unobtrusive@3.2.11
  • Chrome 86.0.4240.111

Steps to reproduce

  • https://jsfiddle.net/1ua63jx0/
  • focus into and out of the date input: it gets a red border, indicating class="input-validation-error"
  • if localization/methods_pt.js is not loaded, then the validation succeeds -- even though our actual date format conforms to methods_pt

This is because date input value is always in ISO format, regardless of the display format (reference)

Expected behaviour

  • normalizeAttributeRule should match date input type with dateISO rule, instead of date rule.
@PatrickHollweck
Copy link

PatrickHollweck commented Apr 27, 2021

Yep, We are having the same issue. Given an <input type="date" /> its .val() is always in the format 01-01-1970.

When we activate the localized methods for Germany the date method validates against the format 01.01.1970 which is the format that is commonly used in Germany, but the format of the input element remains the same which causes the validation to always fail...

This has been open for quite some time now, so I do not expect it to get fixed any time soon. This forces us to not use the localized validation methods...

I would like to help contribute a fix, but I don't think there is an easy approach to fixing this without causing breakage.

Some ideas:

  • Preprocess the value before it gets to the validation method
  • Change the default validation rule for <input type="date" /> to formDate which uses a validation against 01-01-1970
    • This would be the easiest since it would most likely not break any existing code

I wrote a really dirty script that fixes this issue -- incase someone needs this...

// For more information see: https://github.com/jquery-validation/jquery-validation/issues/2359

$(function () {
    $.validator.addMethod("formDate", function (value, element) {
        var iso8601DateRegex = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;

        return (
            $(element).is("input[type=date]") &&
            iso8601DateRegex.test(value)
        );
    }, "This value does not match the ISO8601 form specification");

    var rulesOriginal = $.prototype.rules;
    $.prototype.rules = function () {
        if (arguments.length === 0 && this.is("input[type=date]")) {
            var originalRules = rulesOriginal.apply(this);
            delete originalRules["date"];
            originalRules["formDate"] = true;

            return originalRules;
        }

        return rulesOriginal.apply(this, arguments);
    }
})

@tyomitch
Copy link
Contributor Author

@PatrickHollweck out of curiosity, does my one-line fix at #2360 not help?

@PatrickHollweck
Copy link

PatrickHollweck commented Apr 27, 2021

@tyomitch For various reasons, I cannot modify the jquery-validation script, Therefore I needed a solution that works without modifying the original source...

Have not tested your change, but if it works it would be really nice if someone could merge it :)

@github-actions
Copy link

github-actions bot commented May 6, 2022

This issue/proposal has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automatically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and one of the maintainers will (try!) to follow up.
Thank you for contributing :)

@github-actions github-actions bot added the stale Used to mark stale issues label May 6, 2022
@tyomitch
Copy link
Contributor Author

tyomitch commented May 6, 2022

I think this is wrong, and the problem still persists.

bytestream added a commit that referenced this issue May 6, 2022
* Core: Fix code style to pacify jscs

* Core: Fix validation for input type="date" (#2359)

Test case contributed by @nenadvicentic

Co-authored-by: Kieran <kieran@supportpal.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Used to mark stale issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants