Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strings

Status
Not open for further replies.

arpan

Programmer
Oct 16, 2002
336
IN
Suppose I have the following string :

Code:
(mymail@server.com)

Please note that above string is enclosed in braces. Using JavaScript, how do I get rid of the 2 brackets - ( & ) i.e. the string should be just

Code:
mymail@server.com

Thanks,

Arpan
 
arpan,

Here's an example using reg exp's;

Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<html><head><title>Reg Exp</title>
<script langauge=&quot;JavaScript&quot;>
function strp(str){
re = /(\()(.*)(\))/;
nwstr = str.replace(re,&quot;$2&quot;);
alert(nwstr);
}	 
</script></head><body>
<input type=&quot;text&quot; id=&quot;seek&quot; size=&quot;35&quot;
 onChange=&quot;strp(this.value)&quot;>
</body></html>

2b||!2b
 
Hi arpan me again,
We'll leave the other thread open.
Adam may have a much better solution for your duplicate
problem. This uses &quot;indexOf&quot; to see if the value already
exists in the textbox..
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<html><head><title>Reg Exp</title>
<script langauge=&quot;JavaScript&quot;>
function cktxt(frm,el,opt){
if(frm.t.value.indexOf(el.options[opt].text)!= -1){
  alert(&quot;You already picked that one..try again.&quot;);
  return; }
else { 
frm.t.value += el.options[opt].text+';';
return; }
}	 
</script></head><body>
<form>
<input type=&quot;text&quot; name=&quot;t&quot; size=&quot;35&quot;>
<select onchange=&quot;cktxt(this.form,this,this.selectedIndex)&quot;>
<option>
<option value=&quot;1&quot;>james@bond.com
<option value=&quot;2&quot;>matt@horne.com
<option value=&quot;3&quot;>hugh@grant.com
</select></form></body></html>

2b||!2b
 
why make it so complicated?
Code:
var str = &quot;(mymail@server.com)&quot;;
str = str.replace(/\(|\)/g, &quot;&quot;);

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Thanks, my dear friends, for all your suggestions & inputs but here's the simplest way to do it:

Code:
<script language=&quot;JavaScript&quot;>
var str=&quot;(mymail@server.com)&quot;
str=str.substring(1,str.length-1)
</script>

No RegExp, no replace()! Just substring().....that's it!!!

Regards,

Arpan
 
Hello Lrnmore,

I tried out your idea regarding my 'duplicate' query. It works fine but there's a small problem. The problem is as soon as the empty textbox gets populated with the 1st mail address, a semi-colon (;) also gets appended at the end. I want the semi-colon to be appended if & only if the user selects another e-mail address. Here's an example:

User 1st selects
Code:
james@bond.com
. The textbox should now display

Code:
james@bond.com

But using your code, the textbox now displays

Code:
james@bond.com;

Any workaround to this???

Arpan
 
Jeff,
Another good one there.

arpan,
Nice solution, very clean.
See if this will do for the &quot;;&quot;..
Code:
function cktxt(frm,el,opt){
if(frm.t.value.indexOf(el.options[opt].text)!= -1){
  alert(&quot;You already picked that one..try again.&quot;);
  return; }
else {
	if(frm.t.value.length > 1){
	frm.t.value += ';'+el.options[opt].text;
	return; }
	if(frm.t.value.length < 1){
	frm.t.value += el.options[opt].text;
	return; } }
}

2b||!2b
 
Hi Lrnmore,

Thanks for your help once again. It works great; this is what I was exactly looking out for!!

Regards,

Arpan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top