驗證碼控件WebValidates的使用步驟以下:
1. 將驗證碼控件放入工具箱。
2. 拖放控件到頁面相應位置。
3. 頁面初始化時,編程生成驗證碼(假設驗證碼控件ID爲snCode)。
snCode.Create();//首次加載生成新驗證碼
4. 編碼對比用戶的輸入(假設用戶輸入驗證碼的文本框ID是txtCode),並作相應的處理。
snCode.CheckSN(txtCode.Text.Trim());//返回bool型的值.
示例代碼:
實現驗證碼方式的用戶註冊功能的代碼以下:
<%@ Register Assembly="WebValidates" Namespace="WebValidates" TagPrefix="cc1" %>
Web頁面上增長的內容:
<table>
<tr>
<td width="24%" height="26" align="center" valign="middle">
驗證碼:</td>
<td valign="top" width="37%" align="left">
<asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
<cc1:SerialNumber ID="snCode" runat="server">
</cc1:SerialNumber>
</td>
<td>
</td>
</tr>
<tr>
<td width="24%" height="26" align="center" valign="top">
</td>
<td width="37%" align="left" valign="middle">
<asp:Button ID="btnSubmit" runat="server" Height="31px"
onclick="btnSubmit_Click" Text="提交驗證" Width="124px" />
</td>
<td>
</td>
</tr>
<tr>
<td width="24%" height="26" align="center" valign="top">
</td>
<td valign="top" width="37%" align="left">
<aspabel ID="lblMessage" runat="server"></asp
abel>
</td>
<td>
</td>
</tr>
</table>
後臺代碼:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
snCode.Create();//首次加載生成新驗證碼
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (!CheckCode())
{
lblMessage.Text = "驗證碼錯誤!";
}
else
{
lblMessage.Text = "驗證碼正確!";
}
}
protected bool CheckCode()//驗證方法
{
if (snCode.CheckSN(txtCode.Text.Trim()))//判斷驗證碼是否輸入正確
{
return true;
}
else
{
//snCode.Create();//若是驗證碼輸入不正確,則生成新的驗證碼
return false;
}
}web