attribute dependent on another field

后端 未结 5 1552
抹茶落季
抹茶落季 2020-11-27 04:03

In a model of my ASP.NET MVC application I would like validate a textbox as required only if a specific checkbox is checked.

Something like

public bo         


        
5条回答
  •  情深已故
    2020-11-27 04:49

    Just use the Foolproof validation library that is available on Codeplex: https://foolproof.codeplex.com/

    It supports, amongst others, the following "requiredif" validation attributes / decorations:

    [RequiredIf]
    [RequiredIfNot]
    [RequiredIfTrue]
    [RequiredIfFalse]
    [RequiredIfEmpty]
    [RequiredIfNotEmpty]
    [RequiredIfRegExMatch]
    [RequiredIfNotRegExMatch]
    

    To get started is easy:

    1. Download the package from the provided link
    2. Add a reference to the included .dll file
    3. Import the included javascript files
    4. Ensure that your views references the included javascript files from within its HTML for unobtrusive javascript and jquery validation.

提交回复
热议问题