Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii to everyone,
for the last 2weeks i started learning MVC3,i started preparing a registration page using MVC3 Razor view,here user need to select area of intrest, for this i placed 4 checkboxes.My requirement is i need to restrict the user to select only one checkbox at any point of time, how can i achieve this....also try to tell me the possible events associated with checkbox in mvc3 Razor


Thanks
Ramu
Posted
Updated 29-Nov-17 23:44pm
Comments
vijay__p 26-Apr-13 6:05am    
IF you want user to restrict on only one selection then why not to use RadioButton instead of Checkbox control.
Ram7 from Hyderabad 26-Apr-13 6:19am    
Dear Vijay,thanks for ur responce my requirement is with checkbox only,i will try with ur code,thanks once again for ur responce......

And still if you want to go with checkboxes then checkout sample on jsFiddle
http://jsfiddle.net/vijaypatel/KtPsQ/[^]

Use below function to restrict user on single selection

HTML
<input type="checkbox" id="chk1" class="chk" name="chk1" /> checkbox 1
<input type="checkbox" id="chk2" class="chk" name="chk2" /> checkbox 2
<input type="checkbox" id="chk3" class="chk" name="chk3" /> checkbox 3
<input type="checkbox" id="chk4" class="chk" name="chk4" /> checkbox 4


C#
$('input.chk').change(function(){
        if($('input.chk').length > 1){
            $('input.chk').prop('checked',false);
            $(this).prop('checked',true);
        }
    });
 
Share this answer
 
Comments
Zoltán Zörgő 26-Apr-13 6:49am    
It is working but still not a good answer. Why? Because it does not fit into the practices of ASP.NET MVCx (like unobstructive validation), but first of all, because it is a "client side only" solution. What if user disables JavaScript, checks all, and posts the form.
vijay__p 26-Apr-13 7:05am    
True, Server side validation is required if data is critical. There are possibilities that user has disabled JavaScript and it is not handled in application then user should be redirect to some page and needs to be notified to enable it.
And don't you think so if JavaScript is disable then most of the functionality of application stop working? :)
Zoltán Zörgő 26-Apr-13 13:11pm    
The one disabling JavaScript is either developer, hacker, or in high security environment. You don't have to disable JS for ever, you can disable just to bypass such validation steps.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900