PDA

View Full Version : Javascript Form Spaces Question



theflyingminstr
Sep 6th, 2007, 05:40 AM
Hi, I have a Javscript form input question. I am assuming the Ltrim/Rtrim functions would be suitable for what I am trying to do? In the following code, does anyone know how after the search and replace scheme has been completed, the blank spaces can be removed (or at least the space at the beginning)?

Thanks.


<html>

<head>

<script type = "text/javascript">

function replaceChars() {
var temp = document.subform.text.value;
temp = ' ' + temp + ' ';


temp = temp.replace(/\shello\s/gi, " goodbye ");
temp = temp.replace (/\sthis\s/gi, " that ");
temp = temp.replace (/\stoday\s/gi, " tomorrow ");


document.subform.text.value = temp;
}

</script>

</head>

<body>


<form name="subform">

<textarea rows="8" name="text" cols="31" value=""></textarea>
<br>
<input type=button name=action value="Change" onClick="replaceChars(document.subform.text.value);">


</form>
</body>

Philip M
Sep 6th, 2007, 08:24 AM
temp = " xxx yyy zzz "
temp = temp replace(/\s/g, "") // remove all spaces
temp = temp.replace (/^\s+/, "") //. remove leading space(s)
temp = temp.replace ((^\s+)(\s+$), "") remove leading and trailing spaces

Posts by user:23
Thanks:0

You can test your regular expressions at: http://www.ogauge.co.uk/regextester.html

theflyingminstr
Sep 7th, 2007, 12:22 AM
Oh nice, thank you so much!