View Full Version : remove invalid characters?

10-31-2006, 09:38 PM

I'm using this function from validate the characters in a string.

function validated(string) {
for (var i=0, output='', valid="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.,"; i<string.length; i++)
if (valid.indexOf(string.charAt(i)) != 0)
output += string.charAt(i)
return output;

this works great for removing the bad characters, my problem is that it also removes all spaces in the string.

so my question is, how can i keep the function for removing the bad characters, but maintain the spaces in the string?

thanks in advance for your time!

10-31-2006, 10:12 PM
Chanhe your function for something like this:

function validated(string) {
valid="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .,";
for (var i=0; i<string.length; i++)
if (valid.indexOf(string.charAt(i))!==-1)
return output;
}Note that indexOf() returns -1 if the substring cannot be found.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum