[英]Required Field Validator Issue
我在一个文本框上应用了日期选择器。 并且文本框为必填字段。 问题是,即使选择了日期之后,所需的字段验证器消息也不会显示。 请参阅我的代码以供参考:
<asp:TextBox ID="txtdob" runat="server" CssClass="txtfld-popup1"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqDOB" runat="server" ControlToValidate="txtdob" ErrorMessage="*"></asp:RequiredFieldValidator>
<cc1:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender5" runat="server" TargetControlID="txtdob" WatermarkText="Enter your Date of Birth"></cc1:TextBoxWatermarkExtender>
还请找到我调用的Datepicker的JS:
<script type="text/javascript">
$(function () {
$("#txtdob").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'MM dd, yy',
minDate: "-58Y",
maxDate: "-18Y",
yearRange: "-58:-18",
showOn: "button",
buttonImage: "../images/cal.gif",
buttonImageOnly: true,
showOn: "both"
});
$("#txtdob").on('keydown', function (e) {
e.preventDefault();
e.stopImmediatePropagation();
return false;
});
$("#txtdob").on('cut copy paste', function (e) {
e.preventDefault();
e.stopImmediatePropagation();
return false;
});
$("#format").change(function () {
$("#txtdob").datepicker("option", "dateFormat", $(this).val());
});
$("#btnSubmit").on("click", function () {
$('#lblDOB').text("");
var isValid = Page_ClientValidate("");
if (isValid) {
var dob = Date.parse($("#txtdob").val());
if (isNaN(dob)) {
$('#lblDOB').text("Enter Proper Date of Birth.");
$('#lblDOB').css({ 'color': 'red' });
return false;
}
}
});
});
</script>
请帮忙。
“也请找到我调用的Datepicker的JS”,不确定您的意思。 要触发onchange
事件,输入必须首先模糊。 使用oninput
事件可提高准确性。
$("#txtdob").on('input', function (e) {
e.preventDefault();
e.stopImmediatePropagation();
return false;
});
我尝试从文本框中删除水印,并且按照我的要求接受了验证。
<script type="text/javascript">
$(function () {
$("input[id$=txtdob]").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'MM dd, yy',
minDate: "-58Y",
maxDate: "+0D",
yearRange: "-58:+0",
showOn: "button",
buttonImage: "../images/cal.gif",
buttonImageOnly: true,
showOn: "both"
});
$("#txtdob").on('keydown', function (e) {
e.preventDefault();
e.stopImmediatePropagation();
return false;
});
$("#txtdob").on('cut copy paste', function (e) {
e.preventDefault();
e.stopImmediatePropagation();
return false;
});
$("#format").change(function () {
$("#txtdob").datepicker("option", "dateFormat", $(this).val());
});
});
</script>
另外,请参阅相关文本框的HTML:
<asp:TextBox ID="txtdob" runat="server" CssClass="txtfld-popup1"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqDOB" runat="server" ControlToValidate="txtdob" ErrorMessage="Please enter yout date of birth"></asp:RequiredFieldValidator>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.