Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MVC Razor
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 26-Apr-13 0:59am
Comments
patel_vijay at 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 at 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......
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
 
<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
 
$('input.chk').change(function(){
        if($('input.chk').length > 1){
            $('input.chk').prop('checked',false);
            $(this).prop('checked',true);
        }
    });
  Permalink  
Comments
Zoltán Zörgő at 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.
patel_vijay at 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ő at 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)

  Print Answers RSS
0 OriginalGriff 195
1 Afzaal Ahmad Zeeshan 152
2 ProgramFOX 130
3 Maciej Los 130
4 den2k88 80
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 26 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100