Groups | Blog | Home
all groups > flash actionscript > july 2006 >

flash actionscript : Check fields before submit



philzzz
7/23/2006 10:50:37 PM
Hi, I have a contact page on my website. The submit button checks if some
fields are empty and if so displays a message to "please fill in all
information boxes". However, I would like Flash to verify that there is a "@"
sign, a ".com" or a ".ca" in the e-mail field to make sure the users put in an
email address.

Thank you very much.
Craig Grummitt
7/24/2006 12:40:57 AM
use String.indexOf to test whether a value exists within your string. If it
does not exist, String.indexOf returns -1. If it does exist, it returns the
position of the value.

eg.

if (emailAddress.indexOf("@")>-1) {
//there is an @ symbol inside the string emailAddress
}
philzzz
7/24/2006 3:44:40 PM
Hi, thanks for helping! Not quite sure how to use it thought.. This coding
seems not to be working...

frame 2 displays a message to the user saying to fill in all boxes correctly.



on (release) {
if (nname eq "" or ssubject eq "" or mmessage eq "" or eemail eq "") {
gotoAndStop(2);
}
else if (eemail.indexOf("@" and ".com" or ".ca")>-1) {
gotoAndStop(2);
}
else {
loadVariablesNum("form.php", 0, "POST");
gotoAndStop(3);
}
}
Craig Grummitt
7/24/2006 11:24:50 PM
close. try:

(eq is deprecated in favour of ==)

if (nname == "" or ssubject == "" or mmessage == "" or eemail == "") {
gotoAndStop(2);
} else if (eemail.indexOf("@")>-1 and (eemail.indexOf(".com")>-1 or
eemail.indexOf(".ca")>-1)) {
gotoAndStop(2);
} else {
loadVariablesNum("form.php", 0, "POST");
gotoAndStop(3);
}
philzzz
7/26/2006 1:59:05 PM
Hi, i dont know why but this seems to work:

else if (eemail.indexOf("@")<1 or eemail.indexOf(".ca")<1 and eemail.indexOf(".com")<1) {

Craig Grummitt
7/26/2006 2:46:05 PM
sorry you're right i made a mistake there.
[quoted text, click to view]

if the form is wrong for these reasons, tell them it's wrong.
otherwise if the email address is correct, tell them it's wrong.
otherwise, its all good - let's process it...

hmm... bit of a glaring error in the middle sentence when you write it out
like that. so anyway, to resolve this, you could either simply add a 'not' to
the middle sentence:

otherwise if the email address is NOT correct, tell them it's wrong.

or, in code form:

} else if (!(eemail.indexOf("@")>-1 and (eemail.indexOf(".com")>-1 or
eemail.indexOf(".ca")>-1))) {

or you could convert the statement to become:

} else if (eemail.indexOf("@")==-1 or (eemail.indexOf(".com")==-1 and
eemail.indexOf(".ca")==-1)) {

which in reality is pretty close to what you had anyway. && (and) has a higher
operator precedence than || (or) so the brackets aren't necessary.

an "@" symbol or a ".com" or ".ca" in the first character of a string still
wouldn't make a valid email address, so your suggestion of checking that these
characters are at least in the second character of the string makes sense.

Which all is a long-winded explanation of why the code you've described works!
AddThis Social Bookmark Button