简体   繁体   中英

ASP.NET MVC Checkbox List Validation

as of right now I am validating checkbox list in the model by overriding the IsValid function so it will get validated on the server-side, but I was wondering if there's a way to add validation on the client-side. Thank you.

You can use jquery validate unobtrusive script. For this you would require to put DataAnnotation attributes to your model class

something like following

public class Model
{
    [Required(ErrorMessage="Name is required")]
    [StringLength(50, ErrorMessage = "Name can not be greater than 50")]
    public string Name{ get; set; }
}

And include the following scripts in your view

<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM