Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: spaces in strings


Message #1 by "Raghuram Kalletla" <raghu@m...> on Wed, 27 Dec 2000 03:57:21 -0600
You may be better off using a regular expression to achieve this.

-----Original Message-----
From: raghu@m... [mailto:raghu@m...]
Sent: 27 December 2000 09:57
To: javascript@p...
Subject: [javascript] spaces in strings




Hi,
     I've written a function which inspects a string (an email address) 
for space(s), deletes the space(s) if found and rewrites the string 
back in a textbox. This function is called in the onBlur event of the 
text box. Here is the function:
 
function chopSpace(email)
 
  var theString = document.contact.emailaddress.value;   //where 
contact = name of form, emailaddress = name of textbox 
  var theAddress = theString.replace(" ","")
  document.contact.emailaddress.value = theAddress;
  return true;
 
 
As you can see, the function gets rid of one space only. What if there 
are multiple spaces in the string? To solve this problem, I re-wrote 
the function thus:
 
function chopSpace(email)
 
  var theString = document.contact.emailaddress.value;    //where 
contact = name of form, emailaddress = name of textbox  
  var theSpace = " ";
  var theAddress;
  for (theSpace in theString)
   
    theAddress = theString.replace(" ","")
   
  document.contact.emailaddress.value = theAddress;
  return true;
 
 
But after rewriting the function, the textbox now says "undefined" 
after the function is executed. What am I doing wrong? Thanks for any 
help.
 
Raghu

--- 
NEED TECHNICAL TIPS, TOOLS, AND INSIGHTS?  Is FREE okay?
Visit EarthWeb for the latest in IT Management, Software Development, 
Web Development, Networking & Communications, and Hardware & Systems.  
Click on http://www.earthweb.com for FREE articles, tutorials,
and discussions from the experts.
---
You are currently subscribed to javascript as: $subst('Recip.EmailAddr')
To unsubscribe send a blank email to leave-javascript-$subst('Recip.MemberIDChar')@p2p.wrox.com

  Return to Index