Maxlength Attribute Not Generating Client-Side Validation Attributes

MaxLength Attribute not generating client-side validation attributes

Try using the [StringLength] attribute:

[Required(ErrorMessage = "Name is required.")]
[StringLength(40, ErrorMessage = "Name cannot be longer than 40 characters.")]
public string Name { get; set; }

That's for validation purposes. If you want to set for example the maxlength attribute on the input you could write a custom data annotations metadata provider as shown in this post and customize the default templates.

MVC Validation model attributes: Client side validation not occurring, only server side

Are you including the jquery validation library in your BungleConfig.cs class ? If so, are you including that script bundle in your layout file? It should be working given the HTML that it's producing

String MinLength and MaxLength validation don't work (asp.net mvc)

MaxLength is used for the Entity Framework to decide how large to make a string value field when it creates the database.

From MSDN:

Specifies the maximum length of array
or string data allowed in a property.

StringLength is a data annotation that will be used for validation of user input.

From MSDN:

Specifies the minimum and maximum
length of characters that are allowed
in a data field.

Non Customized

Use [String Length]

[RegularExpression(@"^.{3,}$", ErrorMessage = "Minimum 3 characters required")]
[Required(ErrorMessage = "Required")]
[StringLength(30, MinimumLength = 3, ErrorMessage = "Maximum 30 characters")]

30 is the Max Length
Minimum length = 3

Customized StringLengthAttribute Class

public class MyStringLengthAttribute : StringLengthAttribute
{
public MyStringLengthAttribute(int maximumLength)
: base(maximumLength)
{
}

public override bool IsValid(object value)
{
string val = Convert.ToString(value);
if (val.Length < base.MinimumLength)
base.ErrorMessage = "Minimum length should be 3";
if (val.Length > base.MaximumLength)
base.ErrorMessage = "Maximum length should be 6";
return base.IsValid(value);
}
}

public class MyViewModel
{
[MyStringLength(6, MinimumLength = 3)]
public String MyProperty { get; set; }
}

Validator ignoring MaxLength attributes

You need to make 2 changes to have the validation work the way you expect:

1. You have to change the difficulty field to a property.

The Validator class only validates properties, so change the difficulty definition to a property like this:

[MaxLength(1)] public string difficulty { get; set; } = "a_string_that_is_too_long";

2. Specify the validateAllProperties: true parameter to the Validator.TryValidateObject call.

The documentation for Validator.TryValidateObject is not very forthcoming about the fact that, unless you use the overload with validateAllProperties: true, only the Required attribute will be checked. So modify the call like this:

var isValid = Validator.TryValidateObject(recipe,
context,
results,
validateAllProperties: true);

MaxLength attribute and unobtrusive validation

You are not specifying the desired maximum length of the string, what do you expect? This overload of the MaxLength attributes takes an integer as argument which specifies the actual maximum allowed length of the string. When you use the parameter-less constructor it will use the maximum length allowed by the database, not sure why jQuery Validation chose to implement this as -1 though.

Asp.net mvc dataannotation MaxLength validation does not work

Both Attributes are in System.ComponentModel.DataAnnotations namespace

As per Microsoft Official Website [MaxLength] attribute in for Entity Framework
because Entity Framework knows what can be the maximum length of the column in the database in your case(like varchar(80))

Specifies the maximum length of array or string data allowed in a property.

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.maxlengthattribute.aspx

As in one of you comment you have said you are not using Entity Framework in reply with @jackncoke so [MaxLength(80)] will not work

But in second case [StringLength(80)] is working because it does not have any dependency over Entity Framework.

SO [StringLength(80)] will work in both cases if you are using Entity Framework or without it

Specifies the minimum and maximum length of characters that are allowed in a data field.

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.stringlengthattribute.aspx



Related Topics



Leave a reply



Submit