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

后端 未结 2 1075
深忆病人
深忆病人 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:07
    $(function () {
        $.datepicker.setDefaults({
            dateFormat: 'dd/mm/yy'
        });
    });
    
    Then to bind it to the input element:
    $(function () {
        $("#StartDate").datepicker();
    });
    

    Credits Amalea

    0 讨论(0)
  • 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
                }
            }
        })
    ;
    
    0 讨论(0)
提交回复
热议问题