I have written a TextBoxLengthValidator in C#, it's purpose is to validate a textbox and make sure the text length >= MinimunLength and text length <= MaximunLength.
C# Class
[ToolboxData("<{0}:txtLenVal runat=server ErrorMessage=\"txtLenVal\"></{0}:txtLenVal>")]
public class txtLenVal : BaseCompareValidator
{
/// <summary>
/// Specifies the maximum length of text in the TextBox the control it is validating. If this value
/// is less than 0, then inputs of any length are considered valid.
/// </summary>
[Bindable(true), Description("MaximumLength"), Category("Behavior"), DefaultValue(-1)]
public int MaximumLength
{
get
{
object MaxLengthVS = this.ViewState["MaximumLength"];
if (MaxLengthVS != null)
return (int)MaxLengthVS;
else
return -1;
}
set { this.ViewState["MaximumLength"] = value; }
}
/// <summary>
/// Specifies the minimum length of text in the TextBox the control it is validating. If this value
/// is less than 0, then inputs of any length greater then 0 are considered valid.
/// </summary>
[Bindable(true), Description("MinimumLength"), Category("Behavior"), DefaultValue(-1)]
public int MinimumLength
{
get
{
object MinLengthVS = this.ViewState["MinimumLength"];
if (MinLengthVS != null)
return (int)MinLengthVS;
else
return -1;
}
set { this.ViewState["MinimumLength"] = value; }
}
/// <summary>
/// Constructor - Sets Default Values
/// </summary>
public txtLenVal()
{
this.SetFocusOnError = true;
this.Display = ValidatorDisplay.None;
}
#region Overriden Methods
/// <summary>
/// Adds client-side functionality for uplevel browsers by specifying the JavaScript function
/// to call when validating, as well as a needed parameters
/// </summary>
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
if (base.RenderUplevel)
{
writer.AddAttribute("evaluationfunction", "txtLenVal_IsValid");
writer.AddAttribute("maximumlength", this.MaximumLength.ToString());
writer.AddAttribute("minimumlength", this.MinimumLength.ToString());
}
}
/// <summary>
/// Checks to ensure that the ControlToValidate property is set to a TextBox
/// </summary>
protected override bool ControlPropertiesValid()
{
if (!(FindControl(ControlToValidate) is TextBox))
throw new HttpException("Control to Validate of must be a TextBox.");
return ((FindControl(ControlToValidate)) != null);
}
/// <summary>
/// Performs the server-side validation. If MaximumLength and MinimumLength are less than 0, always returns True;
/// otherwise, returns True only if the ControlToValidate's length is less than or equal to the
/// specified MaximumLength and greater than or equal to the MinimumLength.
/// </summary>
protected override bool EvaluateIsValid()
{
if ((this.MaximumLength < 0) && (this.MinimumLength <= 0))
return true;
else
{
string text = ((TextBox)FindControl(ControlToValidate)).Text;
if (this.MaximumLength < 0)
{
if ((text.Length > int.Parse(this.MinimumLength.ToString())) || (text.Length == int.Parse(this.MinimumLength.ToString())))
return true;
else
return false;
}
else if (this.MinimumLength <= 0)
{
if ((text.Length < int.Parse(this.MaximumLength.ToString())) || (text.Length == int.Parse(this.MaximumLength.ToString())))
return true;
else
return false;
}
else
{
if (((text.Length > int.Parse(this.MinimumLength.ToString())) || (text.Length == int.Parse(this.MinimumLength.ToString()))) &&
((text.Length < int.Parse(this.MaximumLength.ToString())) || (text.Length == int.Parse(this.MaximumLength.ToString()))))
return true;
else
return false;
}
}
}
/// <summary>
/// Injects the JavaScript function that performs client-side validation for uplevel browsers.
/// </summary>
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "evaluationfunction", "txtLenVal_IsValid");
if (this.DetermineRenderUplevel() && this.EnableClientScript)
{
if (!this.Page.ClientScript.IsClientScriptBlockRegistered("jsTxtLenVal_IsValid"))
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<script language=\"JavaScript\">");
sb.AppendLine("function txtLenVal_IsValid(val) {");
sb.AppendLine("\tvar value = ValidatorGetValue(val.controltovalidate);");
sb.AppendLine("\tif ((val.maximumlength < 0) && (val.minimumlength <= 0))");
sb.AppendLine("\t\treturn true;");
sb.AppendLine("\telse {");
sb.AppendLine("\t\tif(val.maximumlength < 0) {");
sb.AppendLine("\t\t\tif((value.length > val.minimumlength) || (value.length == val.minimumlength)) { return true; }");
sb.AppendLine("\t\t\telse { return false; }");
sb.AppendLine("\t\t}");
sb.AppendLine("\t\telse if((val.minimumlength < 0) || (val.minimumlength == 0)) {");
sb.AppendLine("\t\t\tif((value.length < val.maximumlength) || (value.length == val.maximumlength)) { return true; }");
sb.AppendLine("\t\t\telse { return false; }");
sb.AppendLine("\t\t}");
sb.AppendLine("\t\telse {");
sb.AppendLine("\t\t\tif(((value.length > val.minimumlength) || (value.length == val.minimumlength)) && ((value.length < val.maximumlength) || (value.length == val.maximumlength))) { return true; }");
sb.AppendLine("\t\t\telse { return false; }");
sb.AppendLine("\t\t}");
sb.AppendLine("\t}");
sb.AppendLine("}");
sb.AppendLine("</script>");
this.Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "jsTxtLenVal_IsValid", sb.ToString());
}
}
}
#endregion
}
The following validation javascript is injected into page with control:
function txtLenVal_IsValid(val) {
var value = ValidatorGetValue(val.controltovalidate);
if ((val.maximumlength < 0) && (val.minimumlength <= 0))
return true;
else {
if(val.maximumlength < 0) {
if((value.length > val.minimumlength) || (value.length == val.minimumlength)) { return true; }
else { return false; }
}
else if((val.minimumlength < 0) || (val.minimumlength == 0)) {
if((value.length < val.maximumlength) || (value.length == val.maximumlength)) { return true; }
else { return false; }
}
else {
if(((value.length > val.minimumlength) || (value.length == val.minimumlength)) && ((value.length < val.maximumlength) || (value.length == val.maximumlength))) { return true; }
else { return false; }
}
}
}
My problem is that with ie9 false is always returned.
Using the debugger I found that val.minimunlength and val.maximunlength == undefined in ie9.
By looking at val.id i figure what element was val, it is below:
<span id="mainContents_lvLink_lvStandardLinkDisplay" evaluationfunction="txtLenVal_IsValid" maximumlength="50" minimumlength="-1" style="display:none;"></span>
ie9 javascript is recogniziing the inherited properties (evaluationfunction) but not my custom properties (minimumlength & maximumlength)
Any ideas as to what i need to adjust or change?
Thanks in advance.
C# Class
[ToolboxData("<{0}:txtLenVal runat=server ErrorMessage=\"txtLenVal\"></{0}:txtLenVal>")]
public class txtLenVal : BaseCompareValidator
{
/// <summary>
/// Specifies the maximum length of text in the TextBox the control it is validating. If this value
/// is less than 0, then inputs of any length are considered valid.
/// </summary>
[Bindable(true), Description("MaximumLength"), Category("Behavior"), DefaultValue(-1)]
public int MaximumLength
{
get
{
object MaxLengthVS = this.ViewState["MaximumLength"];
if (MaxLengthVS != null)
return (int)MaxLengthVS;
else
return -1;
}
set { this.ViewState["MaximumLength"] = value; }
}
/// <summary>
/// Specifies the minimum length of text in the TextBox the control it is validating. If this value
/// is less than 0, then inputs of any length greater then 0 are considered valid.
/// </summary>
[Bindable(true), Description("MinimumLength"), Category("Behavior"), DefaultValue(-1)]
public int MinimumLength
{
get
{
object MinLengthVS = this.ViewState["MinimumLength"];
if (MinLengthVS != null)
return (int)MinLengthVS;
else
return -1;
}
set { this.ViewState["MinimumLength"] = value; }
}
/// <summary>
/// Constructor - Sets Default Values
/// </summary>
public txtLenVal()
{
this.SetFocusOnError = true;
this.Display = ValidatorDisplay.None;
}
#region Overriden Methods
/// <summary>
/// Adds client-side functionality for uplevel browsers by specifying the JavaScript function
/// to call when validating, as well as a needed parameters
/// </summary>
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
if (base.RenderUplevel)
{
writer.AddAttribute("evaluationfunction", "txtLenVal_IsValid");
writer.AddAttribute("maximumlength", this.MaximumLength.ToString());
writer.AddAttribute("minimumlength", this.MinimumLength.ToString());
}
}
/// <summary>
/// Checks to ensure that the ControlToValidate property is set to a TextBox
/// </summary>
protected override bool ControlPropertiesValid()
{
if (!(FindControl(ControlToValidate) is TextBox))
throw new HttpException("Control to Validate of must be a TextBox.");
return ((FindControl(ControlToValidate)) != null);
}
/// <summary>
/// Performs the server-side validation. If MaximumLength and MinimumLength are less than 0, always returns True;
/// otherwise, returns True only if the ControlToValidate's length is less than or equal to the
/// specified MaximumLength and greater than or equal to the MinimumLength.
/// </summary>
protected override bool EvaluateIsValid()
{
if ((this.MaximumLength < 0) && (this.MinimumLength <= 0))
return true;
else
{
string text = ((TextBox)FindControl(ControlToValidate)).Text;
if (this.MaximumLength < 0)
{
if ((text.Length > int.Parse(this.MinimumLength.ToString())) || (text.Length == int.Parse(this.MinimumLength.ToString())))
return true;
else
return false;
}
else if (this.MinimumLength <= 0)
{
if ((text.Length < int.Parse(this.MaximumLength.ToString())) || (text.Length == int.Parse(this.MaximumLength.ToString())))
return true;
else
return false;
}
else
{
if (((text.Length > int.Parse(this.MinimumLength.ToString())) || (text.Length == int.Parse(this.MinimumLength.ToString()))) &&
((text.Length < int.Parse(this.MaximumLength.ToString())) || (text.Length == int.Parse(this.MaximumLength.ToString()))))
return true;
else
return false;
}
}
}
/// <summary>
/// Injects the JavaScript function that performs client-side validation for uplevel browsers.
/// </summary>
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "evaluationfunction", "txtLenVal_IsValid");
if (this.DetermineRenderUplevel() && this.EnableClientScript)
{
if (!this.Page.ClientScript.IsClientScriptBlockRegistered("jsTxtLenVal_IsValid"))
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<script language=\"JavaScript\">");
sb.AppendLine("function txtLenVal_IsValid(val) {");
sb.AppendLine("\tvar value = ValidatorGetValue(val.controltovalidate);");
sb.AppendLine("\tif ((val.maximumlength < 0) && (val.minimumlength <= 0))");
sb.AppendLine("\t\treturn true;");
sb.AppendLine("\telse {");
sb.AppendLine("\t\tif(val.maximumlength < 0) {");
sb.AppendLine("\t\t\tif((value.length > val.minimumlength) || (value.length == val.minimumlength)) { return true; }");
sb.AppendLine("\t\t\telse { return false; }");
sb.AppendLine("\t\t}");
sb.AppendLine("\t\telse if((val.minimumlength < 0) || (val.minimumlength == 0)) {");
sb.AppendLine("\t\t\tif((value.length < val.maximumlength) || (value.length == val.maximumlength)) { return true; }");
sb.AppendLine("\t\t\telse { return false; }");
sb.AppendLine("\t\t}");
sb.AppendLine("\t\telse {");
sb.AppendLine("\t\t\tif(((value.length > val.minimumlength) || (value.length == val.minimumlength)) && ((value.length < val.maximumlength) || (value.length == val.maximumlength))) { return true; }");
sb.AppendLine("\t\t\telse { return false; }");
sb.AppendLine("\t\t}");
sb.AppendLine("\t}");
sb.AppendLine("}");
sb.AppendLine("</script>");
this.Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "jsTxtLenVal_IsValid", sb.ToString());
}
}
}
#endregion
}
The following validation javascript is injected into page with control:
function txtLenVal_IsValid(val) {
var value = ValidatorGetValue(val.controltovalidate);
if ((val.maximumlength < 0) && (val.minimumlength <= 0))
return true;
else {
if(val.maximumlength < 0) {
if((value.length > val.minimumlength) || (value.length == val.minimumlength)) { return true; }
else { return false; }
}
else if((val.minimumlength < 0) || (val.minimumlength == 0)) {
if((value.length < val.maximumlength) || (value.length == val.maximumlength)) { return true; }
else { return false; }
}
else {
if(((value.length > val.minimumlength) || (value.length == val.minimumlength)) && ((value.length < val.maximumlength) || (value.length == val.maximumlength))) { return true; }
else { return false; }
}
}
}
My problem is that with ie9 false is always returned.
Using the debugger I found that val.minimunlength and val.maximunlength == undefined in ie9.
By looking at val.id i figure what element was val, it is below:
<span id="mainContents_lvLink_lvStandardLinkDisplay" evaluationfunction="txtLenVal_IsValid" maximumlength="50" minimumlength="-1" style="display:none;"></span>
ie9 javascript is recogniziing the inherited properties (evaluationfunction) but not my custom properties (minimumlength & maximumlength)
Any ideas as to what i need to adjust or change?
Thanks in advance.