How can trim spaces in all inputs without adding methods or classes?

后端 未结 3 873
离开以前
离开以前 2021-01-07 07:29

I\'m trying to remove blank spaces from the begining and ending of inputs in general without adding a class or id or event

I tried this live demo but is using

3条回答
  •  长发绾君心
    2021-01-07 07:51

    This is one of the easiest way i found :), it uses jquery hope it helps, it uses your function The input have to be only:

    
    

    This automatically change all inputs, or you can asign to a class

    function trim_text(el) {
        el.value = el.value.
        replace(/(^\s*)|(\s*$)/gi, ""). // removes leading and trailing spaces
        replace(/[ ]{2,}/gi, " "). // replaces multiple spaces with one space
        replace(/\n +/, "\n"); // Removes spaces after newlines
        return;
    }
    $(function(){
      $("textarea").change(function() {
        trim_text(this);
      });
    
      $("input").change(function() {
        trim_text(this);
      });
    });
    

提交回复
热议问题