0

I'm trying to integrate some custom validation into my form for specific fields.

I have two, but I'm focusing on yy/mm/dd

for this I was able to find this expression:

^\d{2}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$

Which I added into this piece of jquery, which I am reusing from a Canadian postal code custom validation which works

//date format yy/mm/dd
    jQuery.validator.addMethod("yearMonthDay", function (yymmdd, element) {
        return this.optional(element) ||
            yymmdd.match(/^\d{2}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/);
    }, "Please use the correct format yy/mm/dd.");

This works in the way that I can see the error by the field when someone types in it, but I'm able to submit anything, so this isn't working.

$("#form").validate({
    rules: {
        Birthdate: {
            required: true,
            yearMonthDay: true
        }
    }
});

Can anyone be of assistance to me? Show me where I've gone wrong?

user2596635
  • 381
  • 1
  • 5
  • 17
  • You need to provide more information about the rest of your code/form. If the regex is working but the form doesn't submit, it sounds like the issue has nothing to do with the regex as your question states. – BadHorsie Jan 06 '20 at 16:24
  • the form does submit and it seems to be ignoring this, I can enter anything into the input field, and see the message "Please use the correct format yy/mm/dd." and submit. – user2596635 Jan 06 '20 at 16:31
  • Just change `\-` into `\/` – Toto Jan 06 '20 at 17:39
  • Show the HTML markup of the form so we can know you didn't make any mistakes there. Otherwise, your code is absolutely NOT letting me *"submit anything"* as you've claimed. https://jsfiddle.net/n1e95gbz In other words, the code you posted only shows a problem with your regex (`yy-mm-dd` instead of `yy/mm/dd`), because it's working fine otherwise where validation is not allowing me to submit *"anything"*. – Sparky Jan 06 '20 at 23:32

1 Answers1

2

Could you try changing:

/^\d{2}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/

to

/^\d{2}\/(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])$/

The regex you are using is looking for yy-mm-dd. The one I supplied should be looking for yy/mm/dd