我正在使用此addMethod来验证输入中的日期。它没有正确测试正则表达式,我认为它可能写错了。日期应采用以下格式:dd.mm.yyyy。请帮忙......
$(function() {
$(".msgBtn").click(function() {
var isValid = true;
if (!$("#startDate").valid()) {
isValid = false;
}
if (!$("#endDate").valid()) {
isValid = false;
}
if (!isValid)
return;
$("form#csr-message").submit(); //save button
});
});
$.validator.addMethod(
"formatdata",
function(value, element) {
var i = /(?:0[1-9]|[12][0-9]|3[01])\.(?:0[1-9]|1[0-2])\.(?:19\d\d|20\d\d)/;
return this.optional(element) || i.test(value);
}, "Incorrect format data");
var validator = $("#csr-message")
.validate(
{
rules : {
startDate : {
formatdata : true
},
endDate : {
formatdata : true
}
},
messages : {
startDate : {
formatdata : jQuery
.format("Start date has incorrect format!"),
},
endDate : {
formatdata : jQuery
.format("End date has incorrect format!"),
}
}
}
html:
<form method="post" action="<%=RelativeActionURL.rewrite(formAction)%>" id="csr-message">
...
<input type="text" id="startDate" name="startDate" placeholder="dd.mm.yyyy" value="<fmt:formatDate pattern="dd.MM.yyyy" value="${messageDetails.startDate}" />"/>
...
<input type="text" id="endDate" name="endDate" placeholder="dd.mm.yyyy" value="<fmt:formatDate pattern="dd.MM.yyyy" value="${messageDetails.endDate}"/>"/>
...
</form>
答案 0 :(得分:0)
$(function () {
$.datepicker.setDefaults({
dateFormat: 'dd/mm/yy'
});
});
Then to bind it to the input element:
$(function () {
$("#StartDate").datepicker();
});
积分Amalea
答案 1 :(得分:0)
您可以使用addMethod函数创建自己的自定义验证方法。假设你想验证&#34; dd / mm / yyyy&#34;:
$.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."
);
然后在您的表单上添加:
$('#myForm')
.validate({
rules : {
myDate : {
Mytypedate: true
}
}
})
;