If/Switch <-- none of 'em werk!

This is just drivin me nuts!
I tried If/If else … not working… I tried switch… not working!
Is it Flash or is it just me?

sendername is a textfield… and mail1 is also a textfield


on (release) {
	namefield = sendername.toLowerCase();
	mailfield = mail1.toLowerCase();
	switch (sendername) {
	case "" :
		mailstatus = "Invalid name!";
		break;
	default :
		switch (namefield.indexOf("syko")) {
		case '0' :
			mailstatus = "Invalid name!";
			break;
		default :
			switch (mail1) {
			case '' :
				mailstatus = "E-mail #1 is invalid!";
				break;
			default :
				switch (mail1.indexOf("@")) {
				case '0':
					mailstatus = "E-mail #1 is invalid!";
					break;
				default :
					switch (mail1.indexOf(".")) {
					case '0' :
						mailstatus = "E-mail #1 is invalid!";
					default :
						mailstatus = "OK"
					}
				}
			}
		}
	}
}

:slight_smile:
Just not working!
When I just press submit without filling anything it says "OK"then I type something into the name field… it says “OK”, I delete the text typed and press submit… it says “Invalid name”… I don’t get it!:x

Syko: The organization of your code is confusing, the form validation process can be achieved using a for-in loop to check your input textfields, an if statment that checks weather the property is an instanceof TextField, and another if statement that checks if the property is an empty string. simple way to do it would look like:

for (var prop in form) {
	if (form[prop] instanceof TextField) {
		if (form[prop].text == "" /*|| other conditions goes here*/ ) {
			trace("complete the whole form");
			break;
		}
	}
}

also switch uses a strict equals ( === ) in comparison. Meaning ‘-1’ wont equal -1 (which indexOf would give).

Yeah right, Flash 5’s switch doesn’t use strict equality tho.

Flash 5 didnt have switch. The only switch there was from liveMotion which reduced the switch to if/else statements at compiletime.

Then why Flash MX’s reference is indicating the Availability of the switch statement as of Flash 4? You’re right anyway.

Edit: Does that mean that we can export our movies as flash 5 or 4 with the switch statement which is only supported by mx?

because the reference blows :wink:

yes, I BELIEVE MX will convert as well… but only if authoring in MX and not in Flash 5 or 4. (never tired though)

I’ve never use for-in before! (nor instanceof) That one seems completely new to but I think I understod it pretty well… though it’s not working!
I nedd to make an object before that right? like
form = { name:sendername, mail:mail1 };

This is pretty confusing!

And senocular I can’t say I understand what you mean by -1 won’t equal to -1! :slight_smile:

[size=1]don’t mind me… I’m dumb[/size]

form is the movieclip which contains the textfields.

And what senocular meant is to change ‘-1’ into (-1). (Strict Equality is what the switch statement use, Unlike the If statement, which uses automatic datatype conversion)

so I can replace it with _root.tellafriend and prop with sendername for example!?

… so far I’ve only accomplished the submit button doing nothing!

:slight_smile:

See this example:


this.createTextField("status_txt", -999, 0,0,300,30)
submit_btn.onRelease = function(){
	for (var prop in this._parent) {
        if (this._parent[prop] instanceof TextField && this._parent[prop].type == "input") {
                if (this._parent[prop].text == "" /*|| other conditions goes here*/) {
                      this._parent.status_txt.text = "complete the whole form";
                        break;
				} else {
					this._parent.status_txt.text = "Done"
					// other stuff goes here (Serverside script, etc.).
				}
		}
	}
}

=== (strict equality) also checks if it’s a number, string, etc.

‘-1’ = string ( “-1” is also a string)
-1 = number

This example will let you know the difference:

trace(1 === “1”)//returns false, It’s Strict, no automatic datatype conversion with this operator
trace(1 == “1”)//returns true, unlike the strict equality, there is automatic datatype conversion with this operator

well I got your example working! Thanx a bunch for that but what if the user doesn’t need to complete the whole form, just a couple of textfields!?

Have to go now… I’l see what I can figure out tomorrow!