Click to See Complete Forum and Search --> : simple string manipulation....


Superfly1611
10-06-2003, 04:57 AM
i need to know how to remove the last letter from the end of a string in a textbox.

I've tried..

this.value --;
this.length --;

Can't get any of it to work.

I'm sure it's simple enough but most searches i do on string manipulation are splits and regular expressions.

Anyone?

fyrestrtr
10-06-2003, 05:08 AM
Here is some code that might help you out.

First, the HTML bits :


<input type="text" id="box" />
<a href="#" onclick="check()">check</a>


Now the javascript:


<script type="text/javascript">
/* Code to return the string minus last character */

function check()
{

text = document.getElementById('box').value;
new_text = text.substring(0.text.length-1);
alert(new_text);
}
</script>


Untested, but should work.

More infromation about manipulating strings with javascript (http://www.novell.com/documentation/lg/extendas35/docs/help/books/TechStringsJS.html).

Charles
10-06-2003, 05:19 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>

<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>

<form action="">
<div>
<label>Foo<input type="text" onchange="this.value = this.value.replace(/.$/, '')"></label>
<button type="submit">Submit</button>
</div>
</form>

Superfly1611
10-06-2003, 05:31 AM
Cheers guys

@ fyrestrtr: for some reason my broswer (ie6) kicked up a fuss at the 0.text part of your script.

@Charles - ces't magnifique - worked a treat - Thnx Mate

fyrestrtr
10-06-2003, 07:01 AM
Yeah made a typo. Should have been a comma instead of a .

Oh well :P