JavaScript Form Validation - Struggling With Email Validation
Hi everybody,
This is for a university project so I wouldn't like it completing for me. Any tips or hints would be more than welcome though!
I'm finally getting the feel for form validation but I'm struggling with the email validation part. I've been deleting and swapping bits and pieces for hours now and I can't seem to get it working. Below is the code I've got so far... The "firstname" and "lastname" validation doesn't work with the current code, but when the red text is taken out it works fine. As you may have guessed, the code I'm struggling with has been made red.
Thanks for any help on this matter; very much appreciated!
Kind regards,
Amph.
Code:
function validateFormFields()
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
{
valid = true;
if (document.frmCreateAcc.firstname.value == "")
{
alert("Please enter your first name.");
valid = false;
document.forms[0].firstname.focus()
return false;
}
if (document.frmCreateAcc.lastname.value == "")
{
alert("Please enter your last name.");
valid = false;
document.forms[0].lastname.focus()
return false;
}
with (document.frmCreateAcc.email.value)
{
if (apos<1||dotpos-apos<2)
alert("Please enter a valid email address");
valid = false;
}
else
confirm("Thank you for creating an O2 account.");
return valid;
}
Last edited by Amphidamas; 04-23-2009 at 04:56 PM.
If you are allowed to use regular expressions, that's what I'd do.
Otherwise, you probably want brackets around your email alert and valid=false. I would check maybe if apos==-1 || dotpos<=apos, but there's probably special cases it wouldn't catch, that's what's great about regex.
If you are allowed to use regular expressions, that's what I'd do.
Otherwise, you probably want brackets around your email alert and valid=false. I would check maybe if apos==-1 || dotpos<=apos, but there's probably special cases it wouldn't catch, that's what's great about regex.
Regular expressions are extremely complicated to do. Even if I managed to code it, I would then have to incorporate it into my program which I wouldn't know how to do.
Found this, but don't have a clue how to incorporate it:
EDIT: Reading through it, I'm guessing the RegExp should be "== true" for the test to come back negative? Confusing! But I suspect there is still something else wrong with it, or am I just overcomplicating things?
Code:
function validateFormFields()
{
validateEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
valid = true;
if (document.frmCreateAcc.firstname.value == "")
{
alert("Please enter your first name.");
valid = false;
document.forms[0].firstname.focus()
return false;
}
if (document.frmCreateAcc.lastname.value == "")
{
alert("Please enter your last name.");
valid = false;
document.forms[0].lastname.focus()
return false;
}
if validateEmail.test(document.frmCreateAcc.email.value == false)
{
alert("Please enter a valid email address");
valid = false;
}
else
confirm("Thank you for creating an O2 account.");
return valid;
}
Last edited by Amphidamas; 04-23-2009 at 05:54 PM.
Bookmarks