Parsley JS - Custom error message %s format

故事扮演 提交于 2019-12-24 20:59:00

问题


I'm trying to validate a YYYY-MM-DD date with the following code (parsley plugin), but i wish to show an error message with the %s value in DD/MM/YYYY format. Is there a way to do that? Thx!

<div class='input-group date' id='datetimepicker'>
  <input type='text' name="contact-date" id="contact-date" data-parsley-mindate="2000-01-01" />
</div>



<script>
     window.ParsleyValidator
        .addValidator('mindate', function (value, requirement) {
            // is valid date?
            var timestamp = Date.parse(value),
                minTs = Date.parse(requirement);

            return isNaN(timestamp) ? false : timestamp > minTs;    
        }, 32)
        .addMessage('en', 'mindate', 'This date should be greater than %s');

    $('#myForm').parsley();

    $('#datetimepicker').datetimepicker({
        language:'en'
    });
    </script>

回答1:


You could return a "dynamic" error message is by returning a failed promise from your validateString method. This example uses this technique.




回答2:


Thank you!! I added the following code when the validation fails and it works (it allows me to access the "%s" value to customize the message)

return $.Deferred().reject("custom message");


来源:https://stackoverflow.com/questions/47978667/parsley-js-custom-error-message-s-format

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!