A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: indexOf problem

  1. #1
    Member
    Join Date
    Sep 2000
    Posts
    91
    I can't figure this one out:

    I have a variable "myString"

    The string myString may not contain any other character the a-z and 0-9

    Let's say myString = "23-b" or "23.b" then I want to generate an error. 23b is ok.

    I tried several thing like this:

    valid = "0123456789abcdefghijklmnopqrstuvwxyz"
    if (myString.indexOf(valid) == -1) {
    bCheck = false
    } else {
    bCheck = true
    }

    I know this works fine in javascript, but I can't get it to work in Flash.

    Any ideas on how to solve this one are welcome.

    Greets Rik.

  2. #2
    Senior Member
    Join Date
    Jul 2001
    Posts
    202
    I have a feeling that it's looking for the entire valid string variable.

    You'll either need to make a loop and go through the valid var or put each letter in an array and go through that:

    Code:
    for (var i=0; i<=valid.length; ++i) {
      if (myString.indexOf(valid[i]) == -1) { 
        bCheck = false; 
      } else { 
        bCheck = true;
      } 
    }
    I haven't tested this but you get the idea.

    Leon

  3. #3
    Senior Member
    Join Date
    Jul 2001
    Posts
    202
    DAMMIT! the forum removes some characters - doesn't work anyway. OK, try this:

    Code:
    valid = "0123456789abcdefghijklmnopqrstuvwxyz";
    for (var i=0; i<=valid.length; ++i) {
      currLett = valid.substring(i,i+1);
      if (myString.indexOf(currLett) == -1) { 
        bCheck = false; 
      } else { 
        trace(currLett + " is OK");
        bCheck = true;
      } 
    }
    Leon

  4. #4
    Senior Member
    Join Date
    Jul 2001
    Posts
    202
    *sigh* Sorry - made a real mess of that. Actually tried it out now and this works:

    Code:
    myString = "ababababaasddsffghghfb";
    //myString = "a.babababaasddsffghghfb";
    valid = "0123456789abcdefghijklmnopqrstuvwxyz";
    for (var i = 0; i<=myString.length; ++i) {
    	//trace(currlett);
    	currLett = myString.substring(i, i+1);
    	if (valid.indexOf(currLett) == -1) {
    		//trace(currLett + " NO");
    		bCheck = false;
    	} else {
    		//trace(currLett + " is OK");
    		bCheck = true;
    	}
    }
    Leon (hopefully for the last time)

  5. #5
    Member
    Join Date
    Sep 2000
    Posts
    91
    Thanks, I'll try this in the morning in my actual code and I'll let you know.

    Greets Rik.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center