Disabling maxlength validation in asp.net mvc3

lolol123

New Member
My application is based on asp.net mvc3. I have a text box which takes an amount and has a maxlength 7 chars (max amount user can enter is 1000000). on blur of the text box i am formatting the amount to currency form. so if the user enters 1000000 it will be formatted to $ 1,00,00,00. So formatting makes the input length to 12. When i submit the form i get validation error on this control telling "it is more than 7 chars.".I tried with writing a custom validation attribute MaxValueAttribute which will limit the maximum value that user can enter to 1000000. the problem with the attribute is that it is not disabling user input (as when we user maxlength attribute on text box) after he enters 7 chars.Is there any way i can skip the maxlength validation.Edit 1: Here is the MaximumValueAttribute \[code\] public class MaximumValueAttribute : ValidationAttribute, IClientValidatable { private double MaximumValue { get; set; } public MaximumValueAttribute(double maxValue) { MaximumValue = http://stackoverflow.com/questions/12793256/maxValue; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if ((double)value <= MaximumValue) return null; return new ValidationResult(base.ErrorMessageString); } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { var rule = new ModelClientValidationRule { ErrorMessage = base.ErrorMessageString, ValidationType ="maximumamount", }; rule.ValidationParameters["maximumvalue"] = MaximumValue; yield return rule; } }\[/code\]
 
Back
Top