Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
validation of data-val-length-max doesn't consider trailing spaces #1274
If my input has data-val-length-max="500", and I put in 501 characters, and the 501st character is a regular character, jQuery validation rightfully catches it as invalid. However, if I put in 501 characters, and the 501st character is a whitespace (often happens when text is copied/pasted for example), it does not catch it - it lets it pass as valid.
The problem with this is that databases do consider those trailing spaces as characters... so if you let this through to the database for a varchar(500), you'd get an error. Additionally, MVC server-side validation (ie; check for ModelState.IsValid()) rightfully catches it as invalid, so there is a discrepancy in the behavior between the two. Without server-side validation in place, Entity Framework will throw a DbEntityValidationException when you call SaveChanges() on the context.
It seems to make more sense for jQuery validation to consider the trailing spaces.
I was checking the demo the if i put a white space + one character it does consider 2 characters.
Try it on: Name (required, at least 2 characters)
Anyway, you should try to add a filter class in your MVC. Or handle copy & pasted text and trim it. It looks like a bussiness logic problem.
But, could be fine to add an option for this.