Format date dd.mm.yyyy for input text with jQuery Validation addMethod

后端 未结 2 1076
深忆病人
深忆病人 2021-01-22 00:50

I am having real trouble with this addMethod for validating a date from an input. It doesn\'t test the regex properly and i think it may be written with errors. The date should

2条回答
  •  后悔当初
    2021-01-22 01:28

    You can create your own custom validation method using the addMethod function. Say you wanted to validate "dd/mm/yyyy":

    $.validator.addMethod(
        "Mytypedate",
        function(value, element) {
            // put your own logic here, this is just a (crappy) example
            return value.match(/^\d\d?\/\d\d?\/\d\d\d\d$/);
        },
        "Please enter a date in the format dd/mm/yyyy."
    );
    

    And then on your form add:

    $('#myForm')
        .validate({
            rules : {
                myDate : {
                    Mytypedate: true
                }
            }
        })
    ;
    

提交回复
热议问题