validation of data-val-length-max doesn't consider trailing spaces #1274

Closed
Ef0Dev opened this Issue Sep 18, 2014 · 2 comments

Comments

Projects
None yet
3 participants
@Ef0Dev

Ef0Dev commented Sep 18, 2014

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.

@kafeltz

This comment has been minimized.

Show comment
Hide comment
@kafeltz

kafeltz Sep 18, 2014

I was checking the demo the if i put a white space + one character it does consider 2 characters.

http://jqueryvalidation.org/files/demo/themerollered.html

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.

kafeltz commented Sep 18, 2014

I was checking the demo the if i put a white space + one character it does consider 2 characters.

http://jqueryvalidation.org/files/demo/themerollered.html

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.

@b-t-o

This comment has been minimized.

Show comment
Hide comment
@b-t-o

b-t-o Mar 6, 2015

Hi. This method now don't trim email values, if the maxlength is set.

Entering an email address like "xyz@web.de " will throw an error :(

b-t-o commented Mar 6, 2015

Hi. This method now don't trim email values, if the maxlength is set.

Entering an email address like "xyz@web.de " will throw an error :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment