I had crafted a custom validator for an DNN asp.net web page with a function to validate form elements.
Using the DNN pop-up calendar, with appropriate date formatting according to the browser, had issues with visitors not setting the language within the browser correctly. It was also decided that using 3 dropdownlists would provide a better control of the selection, and less issue with date selection on mobile devices using the pop up calendar.
I added 3 drop down lists to a web page to facilitate date selection.
To ensure that the date was valid I created a custom control to be referenced on the page with validateDate()
Initially I found that the function was firing, but that the status wasn’t being returned.
The JavaScript function was amended, requiring both sender and args parameters, like so:
<script type="text/javascript">
function validateDate(sender,args) {
var ddlDay = $("#<% = ddlTheDay.clientid %>");
var ddlMonth = $("#<% = ddlTheMonth.clientid %>");
var ddlYear = $("#<% = ddlTheYear.clientid %>");
document.getElementById('<% = cvTheDay.clientid %>').style.display = 'none';
document.getElementById('<% = cvTheMonth.clientid %>').style.display = 'none';
document.getElementById('<% = cvTheYear.clientid %>').style.display = 'none';
// check date was selected
if (ddlDay.val() < 0 ||
ddlMonth.val() < 0 ||
ddlYear.val() < 0) { // if ($('.validateDate').hasClass('validDate')){ // $('.validateDate').toggleClass('inValidDate validDate'); //} args.IsValid = false; return false; } // ensure days value correct if (ddlDay.val() > daysInMonth(ddlMonth.val(), ddlYear.val())) {
args.IsValid = false;
return false;
}
// check date is valid
var dobDate = new Date(ddlYear.val(), ddlMonth.val() - 1, ddlDay.val());
if (Object.prototype.toString.call(dobDate) === "[object Date]") {
if (isNaN(dobDate.getTime())) {
// date is not valid
args.IsValid = false;
return false;
}
else {
// date is valid
args.IsValid = true;
return true;
}
}
else {
// not a date
args.IsValid = false;
return false;
}
}
function daysInMonth(month, year) {
return new Date(year, month, 0).getDate();
}
</script>
Shown below are the dropdownlists, together with their custom validators.
<asp:dropdownlist id="ddlTheDay" visible="true" runat="server" name="ddlTheDay" class="ddlTheDay" /> <asp:dropdownlist id="ddlTheMonth" visible="true" runat="server" name="ddlTheMonth" class="ddlTheMonth" /> <asp:dropdownlist id="ddlTheYear" visible="true" runat="server" name="ddlTheYear" class="ddlTheYear" /> <asp:customvalidator id="cvTheDay" runat="server" SetFocusOnError="True" Display="Dynamic" ErrorMessage="The date is invalid!" ControlToValidate="ddlTheDay" ClientValidationFunction="validateDate" /> <asp:customvalidator id="cvTheMonth" runat="server" SetFocusOnError="True" Display="Dynamic" ErrorMessage="The date is invalid!" ControlToValidate="ddlTheMonth" ClientValidationFunction="validateDate" /> <asp:customvalidator id="cvTheYear" runat="server" SetFocusOnError="True" Display="Dynamic" ErrorMessage="The date is invalid!" ControlToValidate="ddlTheYear" ClientValidationFunction="validateDate" /> <div class="validateDate validDate">The date is invalid!</div>
As can be seen above I am using just the one div with the error announcement. Its default setting is within the module’s CSS file module.css, details are below:
.validateDate{color:#d00;font-weight:bold;}
.validDate{display:none;}
.invalidDate{display:block;}


