Friday, September 10, 2010

SSN Number with AutoTab

.aspx

<table border="0" cellpadding="0" id="HTMLTableSSN" runat="server" cellspacing="0" width="180">
<tr>
<td align="left" valign="middle" visible="False">
<asp:Label ID="lblSSN" runat="server" Font-Bold="True" Visible="False"></asp:Label>
</td>
<td runat="server" id="HTMLCellSSNError" valign="top" visible="False">
<asp:Label ID="lblShowSSNError" runat="server" Visible="False"></asp:Label>
</td>
<td align="left" style="width: 40px" valign="middle">
<asp:TextBox ID="txtFirstThree" runat="server" MaxLength="3" Width="25px"></asp:TextBox>
</td>
<td align="left" style="width: 10px" valign="middle">
<asp:Label ID="lblSSNDash1" runat="server" Font-Bold="true" Text="-"></asp:Label>
</td>
<td style="width: 40px" valign="middle">
<asp:TextBox ID="txtSecondTwo" runat="server" MaxLength="2" Width="25px"></asp:TextBox>
</td>
<td align="left" style="width: 10px" valign="middle">
<asp:Label ID="lblSSNDash2" runat="server" Font-Bold="true" Text="-"></asp:Label>
</td>
<td style="width: 80px" valign="middle">
<asp:TextBox ID="txtLastFour" runat="server" MaxLength="4" Width="50px"></asp:TextBox>
</td>
</tr>
</table>


.cs

protected void Page_PreRender(object sender, EventArgs e )
{
String s2 = "<script type='text/javascript'>" + Environment.NewLine
+ "function Tab(currentField, nextField)" + Environment.NewLine
+ "{" + Environment.NewLine
+ "// Determine if the current field's max length has been reached." + Environment.NewLine
+ "if (currentField.value.length == currentField.maxLength)" + Environment.NewLine
+ "{" + Environment.NewLine
+ " // Retreive the next field in the tab sequence, and give it the focus." + Environment.NewLine
+ " nextField.focus();" + Environment.NewLine
+ "}" + Environment.NewLine
+ "}" + Environment.NewLine
+ "</script>";

Page.RegisterClientScriptBlock("_AutoTab", s2);

txtFirstThree.Attributes.Add("onkeyup", "if (!(event.keyCode==9)){ Tab(document.getElementById('" + txtFirstThree.ClientID + "'), document.getElementById('" + txtSecondTwo.ClientID + "')); }");
txtSecondTwo.Attributes.Add("onkeyup", "if (!(event.keyCode==9)){ Tab(document.getElementById('" + txtSecondTwo.ClientID + "'), document.getElementById('" + txtLastFour.ClientID + "')); }");
}

No comments:

Post a Comment