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);
Share:
13,040
Eric Yin
Author by

Eric Yin

Updated on July 19, 2022

Comments

  • Eric Yin
    Eric Yin almost 2 years

    I found all value passed by Model is not trimmed in ASP.net MVC3

    Is there a way to:

    1. 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)
    2. Must before ModelState.IsValid() (because I often found code stucked at weird ModelState.IsValid and later found because the form item did not be trimmed.)

    Thanks.

  • Eric Yin
    Eric Yin about 12 years
    It's a good idea to trim() on client site as well. Thanks for the code
  • aeroson
    aeroson over 7 years
    .auto_trim as user types: jsfiddle.net/f3vpyhnk/73