Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    remove invalid characters?

    Hello,

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

    PHP Code:
    function validated(string) {
        for (var 
    i=0output=''valid="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.,"i<string.lengthi++)
           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!
    -Ken

  • #2
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Chanhe your function for something like this:
    Code:
    function validated(string) {
    	output="";
    	valid="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .,";
        for (var i=0; i<string.length; i++)
           if (valid.indexOf(string.charAt(i))!==-1)
              output+=string.charAt(i);
        return output;
    }
    Note that indexOf() returns -1 if the substring cannot be found.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •