Monday, 30 June 2014

jQuery Validation for Terms and Conditions Checkbox in Asp.Net

6/30/2014 - By Pranav Singh 0

In this article I will show you how you can restrict user to accept term and condition using jquery in asp.net by using jquery validation. In this we will validate when user will click on submit button we will validate that user have selected term & condition checkbox or not. By this you will be able to force user to accept term and condition bu checking checkbox using jquery in asp.net application.

So for this article first we will create a new asp.net article. In this page we will add the jquery library reference.

<script language="javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>

Now I will give you complete code.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TermAndcondition.aspx.cs"
    Inherits="Termandcondation.TermAndcondition" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>jQuery Validation for Terms and Conditions Checkbox in Asp.Net</title>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        function AcceptTermAndcondition() {

            if ($("#<%= CheckBox1.ClientID%>").is(':checked')) {
                return true;
            }
            else {
                alert("Please accept term and condition");
                return false;
            }
        }
    </script>
    <style type="text/css">
        #form1
        {
            color: #FF6600;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Height="135px" Width="251px">Term and condition of services.....</asp:TextBox>
    </div>
    <asp:CheckBox ID="CheckBox1" runat="server" /><a href="#">Term and condition</a>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Submit" OnClientClick="javascript:return AcceptTermAndcondition();"
        OnClick="Button1_Click" />
    <br />
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </form>
</body>
</html>

Below mention jquery function is used for validating the checkbox that user have selected the checkbox or not. If user have not selected the term and condition checkbox or not.

<script language="javascript" type="text/javascript">
        function AcceptTermAndcondition() {

            if ($("#<%= CheckBox1.ClientID%>").is(':checked')) {
                return true;
            }
            else {
                alert("Please accept term and condition");
                return false;
            }
        }
    </script>

In above code we have first checked that checkbox is check or not. If yes then submit the form otherwise show alert message for accepting the detail. Now run the application for output.

On form submit message display code.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Termandcondation
{
    public partial class TermAndcondition : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = "Thanks for accepting term and condition.";
        }
    }
}


DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top