View Full Version : slight problem with this script

08-03-2002, 11:28 PM
i use the following script to make any spaces at the start or end of a form field to be deleted when the focus goes out of the form field. the only problem is if the person presses enter to submit the form, it doesnt remove the spaces for the field they are in. can anyone modify this script to fix this problem? thanks a lot, paul

heres the script:

function trimtext(txt) {
var temptext=txt.value.replace(/\s*/,'');
ets = temptext.substring(temptext.length-1, temptext.length);
while(ets==" ") {
temptext = temptext.substring(0, temptext.length-1);
ets = temptext.substring(temptext.length-1, temptext.length); }
txt.value = temptext; }

<input type="text" onchange="trimtext(this)">

08-05-2002, 04:12 PM
You could call the function onSubmit of the form.

<form name=temp method=post action=mypage.html onSubmit="trimtext('document.temp.textAreaName')">

That way it is called when the user hit's enter, and the browser begins to submit the form.