Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BaseCompareValidator ie9 JavaScript Property Sharing

Status
Not open for further replies.

mazuma360

Programmer
May 2, 2011
1
US
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.
 
I know others are having problems with javascript in IE9 as well.
Is it just IE9 that's a problem? If so you may need to run IE9 in compatibility mode.

also, your stringbuilder/javascript may be better suited for either
Code:
var javascript = @"
<script language=""JavaScript"">
function txtLenVal_IsValid(val) {
  ...
}
</script>
";
this.Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "jsTxtLenVal_IsValid", javascript);
or as part of a resource file.

It will be much easier to read and maintain, then dynamically building static content.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top