"Jim" wrote
Could someone give me an example of a function which will strip all of the
following characters from a text field, and replace the SPACES with
HYPHENS. I have very little knowledge of writing functions, so if you could be
fairly specific, I would be very grateful.
Characters to be stripped:
'
,
.
@
!
"
£
%
(
)
And all spaces to be converted to:
-
<script src="" type="text/javascript">//<![CDATA[
function strip( o ){
o.value = o.value.replace( /[',.@!"£%()]/g, '' ).replace( / /g, '-' );
}
//]]></script>
<form action="">
<input type="text" name="txt"
value="Strip ',.@!"£%() and make ' ' -" />
<input type="button" value="Strip"
onclick="strip( this.form.elements.txt )" />
</form>
There are many ways you can set this up, but most will use regular
expressions as they can do in a single command what might otherwise require
a labyrinth of substring() and charAt() calls. This function strip(), called
by the button in the form, takes an input element as its parameter, and
performs two replace() actions on that element, the first to remove the
forbidden characters, the second to turn spaces into hyphens.
The first argument of each replace() is not a string, but a regular
expression delimited by slashes, and flagged 'global' with the 'g' after the
second slash, to make the regex replace all instances instead of just the
first.
The " in the example string in the text field is an escaped a double
quote character.
hth
ivo