[RESOLVED]edit.cshtml(scaffold), validate database field to accept only 3 value

Hi!

In edit.cshtml(scaffold) I have field1(from database):

<div
class="editor-field">

            @Html.EditorFor(model => model.field1)

            @Html.ValidationMessageFor(model => model.field1)   

</div>

       

    How can I   validate field1(string type)

     to accept only 3  value  ( ex: value1, value2, value3) ?

  Thanks!

Did you meant that comma separted value in a text box?

Hi fvi,

For this requirement, you could use RegularExpressionAttribute class or custom validation.

More information, please refer to:

# RegularExpressionAttribute Class

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.regularexpressionattribute(v=vs.110).aspx

# Custom Unobtrusive jQuery Validation with Data Annotations

http://thewayofcode.wordpress.com/2012/01/18/custom-unobtrusive-jquery-validation-with-data-annotations-in-mvc-3/

Best Regards

Starain Chen

Leave a Reply