View Full Version : email validation code
Iron Skull
01-14-2009, 07:24 PM
I'm trying to find out how exactly I should write the email validation code. The following code is from a book, so I didn't write that.
var address = "me@you.com";
var isValidAddress = false;
var i = 0;
while (i < address.length) {
if (address.charAt(i) == "@") {
isValidAddress = true;
break;
}
i++;
}
To write code that detect the "@" character in the first index position i.e. "@you@me.com" or two characters "you@@me.com" what changes do I need to make to the code above.
I need everything to stop running as soon as "@" is presented at the first position and output "email invalid."
can I use the following?
if(address.CharAt(i) == "@") {
isValidAddress = false;
}
Thanks
CyanBlue
01-14-2009, 09:11 PM
Howdy and Welcome... :)
Maybe you can use the same code here...
http://tutorials.flashvacuum.com/index.php?show=Email101
gefelkafish
01-15-2009, 10:10 AM
This code checks for @ and . in the email.textfield.
if ( (email.text.indexOf("@") == -1 ) || (email.text.indexOf(".") == -1 ) ) {
trace ( "email error" );
}
if ( (email.text.indexOf("@") != -1 ) && (email.text.indexOf(".") != -1 ) {
trace ( "email correct" );
}
Pretty simple, but its a start.
Iron Skull
01-15-2009, 07:40 PM
Thanks CyanBlue,
It's good to know you're PHP guy as well.
Iron Skull
01-15-2009, 07:41 PM
Thanks gefelkafish
Iron Skull
01-15-2009, 09:08 PM
if ( (email.text.indexOf("@") == -1 ) || (email.text.indexOf(".") == -1 ) ) {
trace ( "email error" );
}
Pretty simple, but its a start.
Let me see if I'm reading it correctly. When searching for "@" or "||" "." and the result is equal to "==" zero result "-1" , trace "email error" ?
kool-Aid
01-20-2009, 04:12 AM
-1 = false or doesn't exist in this case. It is looking for those caracters. since every email has an @ and a .
"||" is AS for the word or
here is one i wrote:
stop();
//retrictions on cracter entry in the input fields
form.zip_txt.restrict = "0-9\\-\\ \\";
form.email_txt.restrict = "0-9\\a-z\\A-Z\\_\\-\\@\\.\\";
trace("crap");
form.message_txt.onSetFocus = function() {
Key.removeListener(oKeyListener);
}
form.name_txt.onSetFocus = form.email_txt.onSetFocus = form.zip_txt.onSetFocus = function() {
Key.addListener(oKeyListener);
}
//set focus to the first text field so that the user may imediately begin typing
Selection.setFocus("form.name_text");
/*
submit validation+
check inputs array makes sure all the required fields are filled out.+
If all required fields are not filled out set the focus to the first blank field
*/
function checkInputs(arr:Array){
for (var i=0;i < arr.length; i++){
if(arr[i].text == ""){
trace(arr[i])
Selection.setFocus(arr[i]);
return false;
}
}
return true;
}
/*
enter key activation +
when you hit the enter key this will either
change the focus to the next required form field
or and run the submit function
*/
var oKeyListener:Object = new Object();
oKeyListener.onKeyDown = activateEnterKey;
Key.addListener(oKeyListener);
function activateEnterKey():Void {
if (Key.getCode() == 13) {
submit();
}
};
submit_btn.onRelease = submit;
//event + list the array items
function submit()
{
indexOfAt = form.email.text.indexOf("@");
lastIndexOfDot = form.email.text.lastIndexOf(".");
///------check via var name not instance name
if (!form.email.length || form.email.indexOf("@") == -1 || form.email.indexOf(".") == -1){
Selection.setFocus("form.email");
required1.text = "PLEASE ENTER A VALID EMAIL ADDRESS";
}else{
required1.text="VERIFYING...";
trace("step one passed... running step 2");
if(form.message_txt.text == ""){
form.message = "No additional Questions or comments submitted";
}
////checks all the other required fields VIA INSTANCE NAMES-------------
if (checkInputs([form.name_txt, form.zip_txt])){
// send variables in form movieclip (the textfields)
required1.text= ("SENDING....");
allgood();
trace("all info verified sending message");
}else{
required1.text= ("PLEASE FILL OUT ALL REQUIRED FIELDS");
trace("made it to check inputs then failed");
}
}
}
///if the form passes all the varifications this function gets called
function allgood()
{
form.loadVariables("emailshuze.php", "POST");
form.onData = function()
{
gotoAndStop("sent");
submit_btn._visible = true;
gotoAndStop("sent");
required1.text= ("Message sent!");
}
}
here is a link to the some source files. These files aren't quite as fancy as the above, but will show you what you are looking for
http://www.actionscript.org/forums/showthread.php3?t=170540&highlight=form+validation
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.