Validating checkboxlist asp net
Net MVC4 Razor application using custom server-side and client-side validation.Follow the following steps for validating checkbox in mvc razor.I found a 2001 article on how to do that (Building a Check Box List Validator Control) but it doesn't work properly on ASP. So I have to make some small changes to make it work on ASP. First thing you have create a class that inherits from Base Validator In this we check if the control is null and if the control can be cast to a Check Box List.This is the methods that performs the server side validation in case Java Script is not enabled on the client, or not supported.(For more, see “The impact of Client IDMode=Predictable”.) Look at the HTML generated by the Check Box List control.It has a specific pattern where every because the additional component depends on the row number.Sometimes you want to make sure the that user has selected at least one of the checkbox in the checkbox list, but as aforementioned, if you user a normal Required Field Validator you will get an exception.You could use a Custom Validator control, but a nicer and more reusable approach is to build a custom web control that extends the Base Validator.
If you add a normal Required Field Validator and you want to validate a Check Box List, you will get a runtime exception informing you that the Check Box List cannot be validated.Sometimes, when we build an input processing forms we might come across situations where we need to validate controls based on some conditions.For example, we can make a textbox to be required field only when a checkbox is checked or a dropdownlist value, etc.public class Registration Model //Making Custom attribute for validating checkbox // IClient Validatable for client side Validation public class Must Be True Attribute : Validation Attribute, IClient Validatable Now, you have applied the custom validation to the checkbox.