How to Trim() all inputs by Model in c# MVC
13,040
Just FYI, I also wrote a small JQuery Plug_in for my project to use trim(), startsWith() & endsWith() for all the inputs string from client side.
(function ($) {
String.prototype.trim = function ()
{ return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, "")) };
String.prototype.startsWith = function (str)
{ return (this.match("^" + str) == str) };
String.prototype.endsWith = function (str)
{ return (this.match(str + "$") == str) };
})(jQuery);
Author by
Eric Yin
Updated on July 19, 2022Comments
-
Eric Yin almost 2 years
I found all value passed by Model is not trimmed in ASP.net MVC3
Is there a way to:
- Apply a trim() on every field in Model (all string fields, at least; but all form fields are string before processed by Model, so better trim them all)
- Must before
ModelState.IsValid()
(because I often found code stucked at weirdModelState.IsValid
and later found because the form item did not be trimmed.)
Thanks.
-
Eric Yin about 12 yearsIt's a good idea to trim() on client site as well. Thanks for the code
-
aeroson over 7 years.auto_trim as user types: jsfiddle.net/f3vpyhnk/73