问题
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