I want to trim any spaces at the beginning of the text box and trim any spaces at the end of the text box. Therefore, I found this code on a website that is supposed to remove spaces at the beginning, end, and several spaces between them:
function trim(s) {
s = s.replace(/(^\s*)|(\s*$)/gi,"");
s = s.replace(/[ ]{2,}/gi," ");
s = s.replace(/\n /,"\n");
return s;
}
My problem is that first of all one of the three lines of code is the one where it aligns the spaces in the middle, because I do not need it. But the main question: how to get a text field to access this function?
I tried using onkeypress, but this did not work, below I tried:
<p>Search: <input type="text" name="questioncontent" onkeypress="return trim(s)" /></p>
So, I want, for example, if this phrase is entered in the text box "My Name is Pete". Then he must remove the spaces at the beginning and at the end so that he reads "My Name is Pete". But how do I get this to work?
UPDATE:
Found out that trim () is jQuery, as well as any javascript equivalent for this, which can be manually encoded to remove spaces at the beginning and end of the text field?
source
share