Groups | Blog | Home
all groups > flash actionscript > january 2004 >

flash actionscript : confirming the text?


z00male
1/27/2004 11:17:55 PM
how can i give an error if an invalid e-mail is entered in a input text field?
i know i can do if(bla.text=="lalala")...
but what if i want it to be a character such as "@" that has to be between the letters?

thanks ahead, z00m

stwingy
1/27/2004 11:45:07 PM
Have a look at string.indexOf()

Certified but not by Macromedia!
Jack.
1/27/2004 11:54:32 PM
function validate(address) {
if (address.length>=7){
if (address.indexOf("@")>0){
if ((address.indexOf("@")+2)<address.lastIndexOf(".")){
if (address.lastIndexOf(".")<(address.length-2)){
return (true);
}}}}
return (false);
};

if (!validate(email.text)){
error.text = "Address not valid";
}

regards
z00male
1/28/2004 12:26:27 AM
thank you m8 but please... explain this code =[

thanks ahead, z00m

Jack.
1/28/2004 12:56:02 AM
indexOf() is used to break down a string into its component characters,
you can see if a particular character is present and where it's located in a string.
the string holding the email address is checked for the "@" symbol and "." (dot)
that are unique to email addresses.

if my email address was-- jack_jack@hotmail.com
using "indexOf("@")" would return a 9. (ninth position in my email address)
Counting starts at zero.
The "indexOf(".")" shows that the dot in my email address is at position 17.

If you look for a character and it's not in the string, a "-1" is returned,
hence - indexOf("@")>0,

hth


regards
z00male
1/28/2004 8:16:55 AM
but how do i use the value it returns? :)

Jack
1/28/2004 11:12:50 AM

[quoted text, click to view]

function validate(address) returns boolean TRUE or FALSE when checking the
email string,

add this code to a new movie, place a button on stage with instance name -
btn,

function validate(address){
if(address.length < 7) return false;// min size for an e-mail
var nb1 = address.indexOf("@");
trace("nb1 - "+nb1);
if(nb1 == '-1') return false;
var adr_temp = address.substring(nb1+1,address.length);
trace("adr_temp - "+adr_temp);
var nbdot = adr_temp.lastIndexOf(".");
trace("nbdot - "+nbdot);
var elem1 = address.substring(0,nb1);
trace("elem1 - "+elem1);
var elem2 = adr_temp.substring(0,nbdot);
trace("elem2 - "+elem2);
var elem3 = adr_temp.substring(nbdot+1,address.length);
trace("elem3 - "+elem3);
if((elem1.length==0)||(elem1.indexOf(" ")!=-1)) return false;
if((elem2.length<2)||(elem2.indexOf(" ")!=-1)) return false;
if((elem3.length<2)||(elem3.length>3)||(elem3.indexOf(" ")!=-1)) return
false;
return true;
};

this.createTextField("test",10,10,10,200,30);
test.text = "jack_jack@hotmail.com"; //test with "jack_jack#hotmail,com"

btn.onPress=function(){
if(validate(test.text)){ //// returns true/false
trace("valid");
} else {
trace("invalid");
}
};

z00male
2/3/2004 11:05:14 PM
lol guys u made it much more complicated than what it really is.. i fount out how to use it here is the simple and shor code

if(textinstance.text.indexOf("@") !=-1 && textinstance.text.indexOf(".") !=-1) {
action goes here

thats it..

Jack.
2/3/2004 11:30:04 PM
[quoted text, click to view]

Whatever :)

your code will mail to a@b.c

Hardly Valid :) especially as your question was -
[quoted text, click to view]


regards
AddThis Social Bookmark Button