Format date with jQuery

Asked

Viewed 488 times

0

I am currently formatting a date field with the following mask:

$("input[type='date']").mask('dD/mM/yYYY', {
                                        translation: {
                                                        d: {pattern: /[0-3]/},
                                                        D: {pattern: /[0-9]/},
                                                        m: {pattern: /[0-1]/},
                                                        M: {pattern: /[0-2]/},
                                                        y: {pattern: /[1-2]/},
                                                        Y: {pattern: /[0-9]/},
                                                        Y: {pattern: /[0-9]/},
                                                        Y: {pattern: /[0-9]/}
                                                      }
                                                });

But I would like to at least can prevent that on the first occurrence, if the day starts with digit 3 allow the next digit to be only 0 or 1, which would result on days 30 and 31.

  • What do you mean in the first instance? First time the field gains focus..?

  • You cannot change your mask to (d/mM/yyyy) where d = [0-2][0-9]|3[0-1]?

1 answer

0

You can change use D: {pattern: /[^3][0-9]|3[0-1]/}, or put a single digit in the mask and make a simpler expression: mask('d/mM/yYYY') and use d: {pattern: /[0-2][0-9]|3[0-1]/}.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.