scriptygoddess

26 Jun, 2003

Javascript: Exploding a string into an array

Posted by: Jennifer In: Bookmarks

Found the function below at webreference. It will take a string and explode it into an array.

In my project, I had one field in a form where the user would enter several email addresses seperated by semi-colon's. I wanted to validate that each email address was valid, but first you have to seperate the long string…

function explodeArray(item,delimiter) {
tempArray=new Array(1);
var Count=0;
var tempString=new String(item);
while (tempString.indexOf(delimiter)>0) {
tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
tempString=tempString.substr(tempString.indexOf(delimiter)+1,tempString.length-tempString.indexOf(delimiter)+1);
Count=Count+1
}
tempArray[Count]=tempString;
return tempArray;
}

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

1 Response to "Javascript: Exploding a string into an array"

1 | ian ward

June 26th, 2003 at 4:48 pm

Avatar

wow, not only are you adding tempArray inside that function to the global namespace, but that function is just horrible…

IE and Moz support the split method.

var tokens = str.split(delim);

if your javascript implementation doesn't support split on strings, add it:

String.prototype.split = function(delim)
{
// your split code here. (hopefully using RegExp)
};

now all your strings will have this capability.

Featured Sponsors


  • cliff: hi wonder if you can help me, pls i designed www.kouga.mobi using dreamweaver CS3 and now want to make the phonenumbers into links so that if you
  • jerey: how do i rewrite this because it tried RewriteEngine on #Options +FollowSymlinks RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FIL
  • Jennifer: So I was testing this out to see if maybe something changed in the latest version - I had a test wordpress setup - and just dumped the mycomment funct

About


Advertisements