Hi
As per the Create Captcha Image with Refresh Button in ASP.Net I have created following code.
My problem was, when I press the submit button data will save without captcha image validation.
<div class="form1">
<asp:TextBox ID="TxtName" runat="server" placeholder="Name" CssClass="box" autocomplete="off"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TxtName" Display="Dynamic"
ErrorMessage="* Name required." Font-Size="Small"
ForeColor="red" SetFocusOnError="True"></asp:RequiredFieldValidator>
<asp:TextBox ID="TxtEmail1" runat="server" placeholder="e-mail" CssClass="box" autocomplete="off"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TxtEmail1" Display="Dynamic"
ErrorMessage="* Email required." Font-Size="Small"
ForeColor="red" SetFocusOnError="True"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TxtEmail1" Display="Dynamic"
ErrorMessage="* Email address wrong." Font-Size="Small"
ForeColor="red" SetFocusOnError="True"
ValidationExpression=".*@.*\..*"></asp:RegularExpressionValidator>
<asp:TextBox ID="TxtEmail2" runat="server" placeholder="Confirmed e-mail" CssClass="box" autocomplete="off"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TxtEmail2" Display="Dynamic"
ErrorMessage="* Email required." Font-Size="Small"
ForeColor="red" SetFocusOnError="True"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="TxtEmail2" Display="Dynamic"
ErrorMessage="* Email address wrong." Font-Size="Small"
ForeColor="red" SetFocusOnError="True"
ValidationExpression=".*@.*\..*"></asp:RegularExpressionValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ErrorMessage="* e-mail not match." Font-Size="Small"
ControlToCompare="TxtEmail1" ControlToValidate="TxtEmail2" Display="Dynamic"
ForeColor="Red" SetFocusOnError="True"></asp:CompareValidator>
<asp:TextBox ID="TxtPhone" runat="server" placeholder="Contcat number" CssClass="box" autocomplete="off"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server"
ControlToValidate="TxtPhone" ErrorMessage="* Invalid phone Number. eg. 0094372221836"
Display="Dynamic" SetFocusOnError="True" Font-Size="Small" ForeColor="Red"
ValidationExpression="^(\d|,)*\d*$"></asp:RegularExpressionValidator>
<asp:TextBox ID="TxtMessage" runat="server" placeholder="Message" cols="30" Rows="10" CssClass="box" TextMode="MultiLine" autocomplete="off"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="TxtMessage" Display="Dynamic"
ErrorMessage="* Message required." Font-Size="Small"
ForeColor="red" SetFocusOnError="True"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"
ControlToValidate="TxtMessage" Display="Dynamic"
ErrorMessage="* Maximum 1000 characters only." Font-Size="Small"
ForeColor="red" SetFocusOnError="True"
ValidationExpression="^[\s\S]{0,1000}$"></asp:RegularExpressionValidator>
<div>
<asp:TextBox ID="txtCaptcha" runat="server" CssClass="box" placeholder="Enter below text"></asp:TextBox>
<cc1:captchacontrol id="Captcha1" runat="server" captchabackgroundnoise="Low" captchalength="5"
captchaheight="60" captchawidth="200" captchamintimeout="5" captchamaxtimeout="240"
fontcolor="#D20B0C" noisecolor="#B1B1B1" />
<asp:ImageButton ID="ImageButton1" ImageUrl="~/images/refresh.png" runat="server" CausesValidation="false" />
<asp:CustomValidator ID="CustomValidator1" ErrorMessage="Invalid. Please try again."
OnServerValidate="ValidateCaptcha" runat="server"
Display="Dynamic" Font-Size="Small"
ForeColor="red" SetFocusOnError="True" />
</div>
<asp:Button ID="BtnSend" runat="server" Text="Send" CssClass="btn" />
<asp:HyperLink ID="HlReset" runat="server" CssClass="btn" NavigateUrl="~/Contact.aspx">Reset</asp:HyperLink>
</div>
Dim connWeb As New SqlConnection(ConfigurationManager.ConnectionStrings("constrWeb").ConnectionString)
Protected Sub ValidateCaptcha(sender As Object, e As ServerValidateEventArgs)
Captcha1.ValidateCaptcha(txtCaptcha.Text.Trim())
e.IsValid = Captcha1.UserValidated
If e.IsValid Then
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Valid Captcha!');", True)
End If
End Sub
Protected Sub WebOnlineInquiry()
Dim ID As Integer = 0
Using cmd As New SqlCommand("WebOnlineInquiry_Insert", connWeb)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@WebSiteID", SqlDbType.Int).Value = 1
cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 100).Value = TxtName.Text
cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 100).Value = TxtEmail2.Text
cmd.Parameters.Add("@Message", SqlDbType.NVarChar, 1000).Value = TxtMessage.Text
cmd.Parameters.Add("@Phone", SqlDbType.NVarChar, 20).Value = TxtPhone.Text
cmd.Parameters.Add("@ClientIP", SqlDbType.NVarChar, 100).Value = Request.ServerVariables("remote_addr")
cmd.Connection = connWeb
connWeb.Open()
ID = Convert.ToInt32(cmd.ExecuteScalar())
connWeb.Close()
End Using
Dim message As String = String.Empty
Select Case ID
Case Else
message = "Saved successful. "
Exit Select
End Select
ClientScript.RegisterStartupScript([GetType](), "alert", (Convert.ToString("alert('") & message) + "');", True)
End Sub
Protected Sub BtnSend_Click(sender As Object, e As EventArgs) Handles BtnSend.Click
WebOnlineInquiry()
End Sub
Thank you
samsmuthu