Additional Code - zip file 56 KB

Listing 1

[AspNetHostingPermission(SecurityAction.LinkDemand, 
Level = AspNetHostingPermissionLevel.Minimal),
DefaultProperty("Text"),
ToolboxData("<{0}:PhoneControl runat=\"server\"> ")]
public class PhoneControl : CompositeControl
{
	private Label phoneLabel;
	private TextBox phoneTextBox;
	private RequiredFieldValidator phoneRequired;
	private RegularExpressionValidator phoneRegex;
	private Style labelStyle;
	private Style phoneStyle;
}

Listing 2

[Bindable(true),
Category("Behavior"),
DefaultValue(""),
Description("Validation Group the control belongs to")]
public string ValidationGroup
{
	get
	{
		EnsureChildControls();
		return phoneRequired.ValidationGroup;
	}
	set
	{
		EnsureChildControls();
		phoneRequired.ValidationGroup = value;
		phoneRegex.ValidationGroup = value;
	}
}


Listing 3

protected override void CreateChildControls()
{
	Controls.Clear();

	phoneLabel = new Label();
	phoneLabel.ID = "phoneLabel";
	Controls.Add(phoneLabel);

	phoneTextBox = new TextBox();
	phoneTextBox.ID = "phoneText";
	phoneTextBox.Attributes.Add("onblur", "formatPhone(this)");
	Controls.Add(phoneTextBox);

	phoneRequired = new RequiredFieldValidator();
	phoneRequired.ID = "phoneRequired";
	phoneRequired.ControlToValidate = phoneTextBox.ID;
	phoneRequired.Text = "*";
	Controls.Add(phoneRequired);

	phoneRegex = new RegularExpressionValidator();
	phoneRegex.ID = "phoneRegex";
	phoneRegex.ControlToValidate = phoneTextBox.ID;
	phoneRegex.Text = "*";
	phoneRegex.ValidationExpression =
	@"(((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4})|\d{10}|\d{7}";
	Controls.Add(phoneRegex);
}


Listing 4

private void RenderJavaScript()
{
if(!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "formatPhone")
{
	StringBuilder sb = new StringBuilder();
	sb.Append("function formatPhone(phoneTextBox){\r\n");
	sb.Append("\tvar text = phoneTextBox.value;\r\n");
	sb.Append("\ttext = text.replace(/\\D+/g,\"\");\r\n");
	sb.Append("\tif(text.length == 10){\r\n");
	sb.Append("\t\t phoneTextBox.value = \"(\" + text.substr(0,3) + \") \"
	+ text.substr(3,3) + \"-\" + text.substr(6,4);\r\n");
	sb.Append("\t}\r\n");
	sb.Append("\tif(text.length == 7){\r\n");
	sb.Append("\t\t phoneTextBox.value = text.substr(0,3) + \"-\" + text.substr(3,4);\r\n");
	sb.Append("\t}\r\n");
	sb.Append("}\r\n");
	string script = sb.ToString();
	Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "formatPhone", script, true);
}
}


Listing 5

protected override void Render(HtmlTextWriter writer)
{
	AddAttributesToRender(writer);

	writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "1", false);
	writer.AddAttribute(HtmlTextWriterAttribute.Border, "0", false);
	writer.RenderBeginTag(HtmlTextWriterTag.Table);

	if (labelStyle != null)
	{
phoneLabel.ApplyStyle(labelStyle);
	}

	if (phoneStyle != null)
	{
phoneTextBox.ApplyStyle(phoneStyle);
	}

	writer.RenderBeginTag(HtmlTextWriterTag.Tr);
	writer.RenderBeginTag(HtmlTextWriterTag.Td);
	phoneLabel.RenderControl(writer);
	writer.RenderEndTag();
	writer.RenderBeginTag(HtmlTextWriterTag.Td);
	writer.Write(" ");
	writer.RenderEndTag();
	writer.RenderBeginTag(HtmlTextWriterTag.Td);
	phoneTextBox.RenderControl(writer);
	phoneRequired.RenderControl(writer);
	phoneRegex.RenderControl(writer);
	writer.RenderEndTag();
	writer.RenderEndTag();
	writer.RenderEndTag();
}

Additional Code - zip file 56 KB