Many friends say that deleting spaces in a form can be implemented directly by using the trim function in jquery, but he can only remove spaces at the beginning and end. If I want to remove all spaces in the form, this will not work.
Let's take a look at how to remove spaces Using trim.
The Code is as follows: |
Copy code |
// Used for trimming whitespace TrimLeft =/^ s + /, TrimRight =/s + $ /, // Use native String. trim function wherever possible Trim: trim? Function (text ){ Return text = null? "": Trim. call (text ); }: // Otherwise use our own trimming functionality Function (text ){ Return text = null? "": Text. toString (). replace (trimLeft, ""). replace (trimRight ,""); }, |
Method to remove spaces Using Regular Expressions
The Code is as follows: |
Copy code |
$ ("Input [type = text]"). change (function (){ $ ('Input [type = text]: not (: disabled) '). each (function (){ Var str = $ (this). val (); Str = str. replace (/s/g ,""); $ (This). val (str ); }); });
|