Skip to content

Foolproof cannot perform client-side time validation #18

Description

@cesarsouza

While the library can perform client-side date validation, validating fields marked with [DataType(DataType.Time)] and rendered as times (e.g., in the format "hh : mm") will not validate correctly.

To solve this issue, I performed the following changes to mvcfoolproof.core.js:

  • Added a "isTime" function right above the isDate local function:
    var isTime = function (input) {
        return moment(input, ['h:m a', 'H:m']).isValid();
    };
  • Changed the checks below that region from:
    if (isDate(value1)) {
        value1 = Date.parse(value1);
        value2 = Date.parse(value2);
    } else if (isBool(value1)) {
        if (value1 == "false") value1 = false;
        if (value2 == "false") value2 = false;
        value1 = !!value1;
        value2 = !!value2;
    }
    ...

to

    if (isTime(value1)) {
        value1 = moment(value1, ['h:m a', 'H:m']);
        value2 = moment(value2, ['h:m a', 'H:m']);
    }
    else if (isDate(value1)) {
        value1 = Date.parse(value1);
        value2 = Date.parse(value2);
    }
   ...

and now it works. The bad thing about the above is that I have introduced a dependency on moment.js, I don't know if was already used by the library before.

Hope this can help other people also trying to validate hour/minute intervals.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions