ASP.NET에서 TextBox에서 엔터키를 이용하여 버튼 클릭을 실행하게 하는 기능과 주민 번호 검증에 대한 Validation 예제입니다.
aspx 페이지 소스
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 228px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td class="style2">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
<asp:Label ID="Label1" runat="server" Text="결과"></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="확인" />
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
aspx.cs 소스 파일 내용
public bool ValidateRID(string RID)
{
bool idval = false;
int[] digitWeights = { 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5 };
if (RID.Length != 13)
return false;
try
{
if (int.Parse(RID.Substring(6, 1)) < 1) return false;
if (int.Parse(RID.Substring(6, 1)) > 4) return false;
if (int.Parse(RID.Substring(2, 1)) > 2) return false;
if (int.Parse(RID.Substring(4, 1)) > 4) return false;
int sum = 0;
int i;
for (i = 0; i < RID.Length - 1; i++)
{
sum += int.Parse(RID.Substring(i, 1)) * digitWeights[i];
}
if ((11 - (sum % 11)) % 10 != int.Parse(RID.Substring(12, 1)))
{
return idval;
}
}
catch
{ return false; }
idval = true;
return idval;
}
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Attributes["onkeypress"] = "if(event.keyCode == 13){" + Page.ClientScript.GetPostBackEventReference(Button1, "") + "}";
}
protected void Button1_Click(object sender, EventArgs e)
{
if (ValidateRID(TextBox1.Text))
{
Label1.Text = "주민번호 검증";
}
}