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.
This comment has been removed by the author.
ReplyDeleteThank 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