Validating a date range in a textbox; need to dynamically update the minimum value of the range.
When you use a range validator in ASP.NET, javascript gets generated the first time the page is rendered to set the range, and after postback, setting the range values has no effect on the range used.
Other than rewriting the validation as custom client side script, is there a simpler / better way to accomplish this than what I've done below?
Basically on page load, I get the range validator object, set the minimum value on it then reinvoke validation. Then on any keyup in the textfield it is dependent on I reinvoke this function.
<asp:TextBox ID="txtStartDate" runat="server"
onkeyup="updateStartDate(this.value); return false;">
<asp:RangeValidator ID="rvLDA" ControlToValidate="txtLDA" Type="Date"
ErrorMessage=" Error message here" MinimumValue="1/1/1753"
MaximumValue="12/31/2020" runat="server" Enabled="false" />
...
ctl00_BodyContent_rvLDA.evaluationfunction = "RangeValidatorEvaluateIsValid";
ctl00_BodyContent_rvLDA.maximumvalue = "9/7/2010";
ctl00_BodyContent_rvLDA.minimumvalue = "8/23/2010";
$(document).ready(function(){
var txtSD = document.getElementById("<% =txtStartDate.ClientID %>");
if (null!=txtSD) updateStartDate(txtSD.value);
});
function updateStartDate(newStartDate)
{
if (!isNaN(Date.parse(newStartDate)))
{
var rvLDA = document.getElementById("<% =rvLDA.ClientID %>");
if (null!=rvLDA)
{
if (undefined != rvLDA.isvalid
&& undefined != rvLDA.minimumvalue
&& undefined != rvLDA.evaluationfunction)
{
rvLDA.minimumvalue = newStartDate;
rvLDA.isvalid = rvLDA.evaluationfunction(rvLDA);
rvLDA.style["visibility"] = rvLDA.isvalid ? "hidden" : "visible";
}
}
}
}