Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataAnnotations StringLength Attribute MVC - without max value

I am using Data Annotations for Model validation in MVC4 and am currently using the StringLengthAttribute however i do NOT want to specify a maximum value (currently set at 50) but DO want to specify a minimum string length value.

Is there a way to only specify only a minimum length? Perhaps another attribute i can use?

My current code is:

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Confirm New Password")]
    [StringLength(50, MinimumLength = 7)]
    [CompareAttribute("NewPassword", ErrorMessage = "The New Password and Confirm New Password fields did not match.")]
    public string ConfirmNewPassword { get; set; }
like image 369
Mr-DC Avatar asked Jul 09 '12 23:07

Mr-DC


People also ask

Which data annotation value specifies the max and min length of string with error?

The StringLength Attribute Specifies both the Min and Max length of characters that we can use in a field.

What is data annotation validator attributes in MVC?

Data annotation attributes are attached to the properties of the model class and enforce some validation criteria. They are capable of performing validation on the server side as well as on the client side. This article discusses the basics of using these attributes in an ASP.NET MVC application.


3 Answers

Is there a way to only specify only a minimum length? Perhaps another attribute i can use?

Using the standard data annotation, No. You must specify the MaximumLength. Only the other parameters are optional.

In such a case, I'd recommend something like this:

[StringLength(int.MaxValue, MinimumLength = 7)]

You can also use a Regex (regular expression) attribute like this one:

[RegularExpression(@"^(?:.*[a-z]){7,}$", ErrorMessage = "String length must be greater than or equal 7 characters.")]

More on this here: Password Strength Validation with Regular Expressions

like image 137
Leniel Maccaferri Avatar answered Oct 17 '22 14:10

Leniel Maccaferri


There is the [MinLength(7)] attribute as well for this purpose.

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

like image 30
BigTone Avatar answered Oct 17 '22 16:10

BigTone


Have you thought of removing the data annotations and add an Html attribute to the Html.TextBoxFor element in your View?

Should look something like:

@Html.TextBoxFor(model => model.Full_Name, new { htmlAttributes = new { @class = "form-control", @minlength = "10" } })

or

@Html.TextBoxFor(model => model.Full_Name, new { @class = "form-control",  @minlength = "10" } })

10 being the minimum length you choose.

I like to add html attributes to my view as I can quickly see the impact it has. without messing with your database and does not require you to run migrations and database update if your using migrations (code first approach).

just remember that when you change the EditorFor to TextBoxFor you will loose your styling but should be an easy fix, again you can just add the styles to your view or add styling to your CSS file.

Hope this helps :)

like image 1
Francois Muller Avatar answered Oct 17 '22 14:10

Francois Muller