connectseekers.com

Sunday, 3 August 2014

Validation for Accepting Terms and Conditions Checkbox in Asp.Net MVC Using jQuery

8/03/2014 - By Pranav Singh 2

This article will show you how you can force user to accept the term and condition using jquery in your asp.net mvc application. This article you can use in MVC2, MVC3, MVC4, MVC5 application.

Some of my previous articles are as follows: jQuery Validation for Terms and Conditions Checkbox in Asp.Net.

So for this article first we will create a new asp.net application and add a controller.  In this I have already added get and post method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication4.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(int id = 0)
        {
            @ViewBag.Message = "Thanks for accepting term and condition.";
            return View();
        }
    }
}

Now we will create view for this controller action.

@{
    ViewBag.Title = "Index";
}
<script language="javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script language="javascript" type="text/javascript">
        function AcceptTermAndcondition() {

            if ($("#chkcondition").is(':checked')) {
                return true;
            }
            else {
                alert("Please accept term and condition");
                return false;
            }
        }
    </script>
@using (Html.BeginForm("", ""))
{
 <div>
    @Html.TextArea("Termandcondition", "Term and condition of services.....", new { @height = "135px", @width = "251px", @id = "txtcondition" })
    </div>@Html.CheckBox("Condition",false,new{@id="chkcondition"})<a href="#">Term and condition</a>
    <br />
    <br />
    <input type="submit" value="submit"  onclick="javascript:return AcceptTermAndcondition();"/>
    <br />
    <div style="color:Red">@ViewBag.Message</div>
}

Now we have done in above code I have used jQuery for validating the checkbox. If user have selected the checkbox on that case he will be allowed to make the post, otherwise we will display an alert message to user to accept the term and condition.


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

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thank you so much for this, it helped me a lot in my project. it's straight and forward. would you please post anything on button functions. e.g next button and previous buttons. in MVC

    ReplyDelete

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
Email Id : contact@aspdotnet-pools.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


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top