This project is read-only.

Infusing maxlength html limit to input elements

Jun 11, 2009 at 9:07 AM


I was expecting the StringLengthRule to add traditional html maxlength limit to input fields, but this isn't the case...


...yet :)

Any ideas how can this be achieved?

Preferably without relying on javascript, as this is needed most when javascript is not supported.


Related code:

Related standard:




Jun 17, 2009 at 8:56 AM

Hi Stefan

It isn't possible to do this without JavaScript - xVal doesn't have control over how your input controls are rendered, so it can't insert a maxlength attribute. It would be possible to do it with JavaScript, but xVal doesn't do this at present. Good suggestion though!


Jun 17, 2009 at 9:47 AM

Tnx for the explanation before digging too deep into xVal sources :)

Javascript solution would surely be a welcome, addition to xVal, suitable for most clients.

I guess in extreme cases, where we cannot rely on javascript, we could implement our own input field rendering, with added maxlength html attribute based on the presence of MaxLength data annotation attribute.


Jun 17, 2009 at 11:06 AM


I suggest you look at the excellent series on Opinionated Html Builder for MVC

You can probably do something similar as you suggest.