disclaimer: Im fairly new to web page coding.
I am trying to do some basic form validation. I have 3 fields (firstName, lastName, E-Mail, Comment). The form onSubmit calls my javascript validation function like so.
<form class=”contactUs” name=”ContactUs” action=”mailto:
The validate function is here.
function validate(form)
{
//validate the individual elements to see if they are properly entered
if(form.firstName.value==””)
{
alert(“First Name is a required field, please enter your first name.”);
return false;
}
else if (form.lastName.value==””)
{
alert(“Last Name is a required field, please enter your last name.”);
return false;
}
else if (form.E-Mail.value.indexOf(“@”)==-1 || form.E-Mail.value.indexOf(“@”)==0 || form.E-Mail.value==””)
{
alert(“E-Mail Address is required, please enter your E-Mail address”);
return false;
}
return false;
}
The first and last name validation simply make sure that at least one character has been entered before it allows the form to submit. This part of the code works perfectly. The email portion is the part that is not working. If I take the email validation section and move it into its own javascript file, it works with onBlur. For some reason the same code refuses to work in this file. Is there a better way to do this? Any help you can offer would be appreciated.
[CODE] else if (form.email.value.indexOf(".") <= (form.email.value.indexOf("@"))
{
alert("E-Mail Address is required, please enter your E-Mail address");
return false;
}[/CODE]
[CODE]//validation function for form
function validate(form)
{
//declare local variables
var indexAt = form.email.value.indexOf("@");
var indexDot = form.email.value.indexOf(".");
//validate the first name
if(form.firstName.value=="")
{
alert("First Name is a required field, please enter your first name.");
form.firstName.focus();
return false;
}
//validate the last name
else if (form.lastName.value=="")
{
alert("Last Name is a required field, please enter your last name.");
form.lastName.focus();
return false;
}
//validate the email address
else if (form.email.value.indexOf("@")==-1 || form.email.value.indexOf("@")==0 || form.email.value=="" || form.email.value.indexOf('.')==-1 || form.email.value.indexOf(".")==0)
{
alert("E-Mail Address is required, please enter your E-Mail address");
form.email.focus();
return false;
}
//validate the email address
else if (indexDot <= indexAt)
{
alert("E-Mail Address is required, please enter your E-Mail address");
form.email.focus();
return false;
}
//verify whether the default string is in the comment box
else if (form.Comment.value=="Comment")
{
alert("A Comment is required, please enter a meaningful Comment");
form.Comment.focus();
return false;
}
//verify that the comment box is not empty
else if (form.Comment.value.replace(/[srn]/g,"")=="")
{
alert("A Comment is required, please enter a meaningful Comment");
form.Comment.focus();
return false;
}
//valid entry, submit form
return true;
}[/CODE]
[CODE]//simple function to remove spaces
function removeSpaces(string) {
return string.split(' ').join('');
}
[/CODE]
[CODE]//validation function for form
function validate(form)
{
//declare local variables
var invalidExpression = /[$\@\#%^&*()[]+_{}`~=|]/;
var validCheckFirst = form.firstName.value.search(invalidExpression);
var validCheckLast = form.lastName.value.search(invalidExpression);
var indexAt = form.formal.value.indexOf("@");
var indexDot = form.formal.value.indexOf(".");
//validate the first name
if(form.firstName.value=="")
{
alert("First Name is a required field, please enter your first name.");
form.firstName.focus();
return false;
}
//validate the last name
else if (form.lastName.value=="")
{
alert("Last Name is a required field, please enter your last name.");
form.lastName.focus();
return false;
}
//validate the email address
else if (form.formal.value.indexOf("@")==-1 || form.formal.value.indexOf("@")==0 || form.formal.value=="" || form.formal.value.indexOf('.')==-1 || form.formal.value.indexOf(".")==0)
{
alert("E-Mail Address is required, please enter your E-Mail address");
form.formal.focus();
return false;
}
//validate the email address
else if (indexDot <= indexAt)
{
alert("E-Mail Address is required, please enter your E-Mail address");
form.formal.focus();
return false;
}
//verify whether the default string is in the comment box
else if (form.Comment.value=="Comment")
{
alert("A Comment is required, please enter a meaningful Comment");
form.Comment.focus();
return false;
}
//verify that the comment box is not empty
else if (form.Comment.value.replace(/[srn]/g,"")=="")
{
alert("A Comment is required, please enter a meaningful Comment");
form.Comment.focus();
return false;
}
//validate that illegal characters have not been entered into first name
else if (validCheckFirst != -1)
{
alert("One or more of the characters entered for First Name are not valid, please re-enter your First Name");
return false;
}
//validate that illegal characters have not been entered into last name
else if (validCheckLast != -1)
{
alert("One or more of the characters entered for Last Name are not valid, please re-enter your Last Name");
return false;
}
//valid entry, submit form
return true;
}[/CODE]
[CODE]//simple function to remove spaces
function removeSpaces(string)
{
return String.trim(string);
}[/CODE]
[CODE]//validation function for form
function validate(form)
{
//declare local variables
var invalidExpression = /[$\@\#%^&*()[]+_{}`~=|]/;
var validCheckFirst = form.firstName.value.search(invalidExpression);
var validCheckLast = form.lastName.value.search(invalidExpression);
var indexAt = form.formal.value.indexOf("@");
var indexDot = form.formal.value.indexOf(".");
//validate the first name
if(form.firstName.value=="")
{
alert("First Name is a required field, please enter your first name.");
form.firstName.focus();
return false;
}
//validate the last name
else if (form.lastName.value=="")
{
alert("Last Name is a required field, please enter your last name.");
form.lastName.focus();
return false;
}
//validate the email address
else if (form.formal.value.indexOf("@")==-1 || form.formal.value.indexOf("@")==0 || form.formal.value=="" || form.formal.value.indexOf('.')==-1 || form.formal.value.indexOf(".")==0)
{
alert("E-Mail Address is required, please enter your E-Mail address");
form.formal.focus();
return false;
}
//validate the email address
else if (indexDot <= indexAt)
{
alert("E-Mail Address is required, please enter your E-Mail address");
form.formal.focus();
return false;
}
//verify whether the default string is in the comment box
else if (form.Comment.value=="Comment")
{
alert("A Comment is required, please enter a meaningful Comment");
form.Comment.focus();
return false;
}
//verify that the comment box is not empty
else if (form.Comment.value.replace(/[srn]/g,"")=="")
{
alert("A Comment is required, please enter a meaningful Comment");
form.Comment.focus();
return false;
}
//validate that illegal characters have not been entered into first name
else if (validCheckFirst != -1)
{
alert("One or more of the characters entered for First Name are not valid, please re-enter your First Name");
return false;
}
//validate that illegal characters have not been entered into last name
else if (validCheckLast != -1)
{
alert("One or more of the characters entered for Last Name are not valid, please re-enter your Last Name");
return false;
}
//valid entry, submit form
return true;
}[/CODE]
[CODE]//simple function to remove spaces
function removeSpaces(string)
{
return String.trim(string);
}[/CODE]
[CODE]//this will have to be at the end of my validation function, the only way to return true is to validate all previous statements and finish in this loop
for (i=0; i < endOfString; i++)
{
if (found and before @ symbol)
{
keep searching for a period;
}
else if (found and after @ symbol)
{
return true;
else //end of string reached and no more periods
{
return false
}
} [/CODE]
[CODE]
/*
This function validates an email address' format.
It does not validate whether an email address is real or not.
*/
function validateEmailAddress(emailAddress)
{
if(emailAddress == "") return true; //assume email address is not mandatory
//reject email address if there is no @ or if it contains spaces
if(emailAddress.indexOf("@") == -1 || emailAddress.indexOf(" ") != -1)
{
return false;
}
//reject the email address if there is more than 1 @
var count = 0;
for(var i=0; i < emailAddress.length; i=i+1)
{
if(emailAddress.charAt(i) == "@") {
count = count + 1;
}
if(count > 1)
{
return false;
}
}
//reject the email address if it starts or ends with a dot or an @
if(emailAddress.substring(0,1) == "." || emailAddress.substring(emailAddress.length-1) == "."
|| emailAddress.substring(0,1) == "@" || emailAddress.substring(emailAddress.length-1) == "@")
{
return false;
}
//reject the email address if there is a dot after the @
if(emailAddress.charAt((emailAddress.indexOf("@")+1)) == ".")
{
return false;
}
//now split the email address at the @ synbol
//emailPart[0] contains string to the left of @
//emailPart[1] contains string to the right of @
var emailPart = emailAddress.split("@");
//reject email address if there are 2 dots in a row to the right of @
for(var i=0; i < emailPart[1].length-1; i=i+1)
{
if(emailPart[1].charAt(i) == "." && emailPart[1].charAt(i+1) == ".")
{
return false;
}
}
//split the string on the right of the @ using a dot delimeter
var emailRightPart = emailPart[1].split(".");
//reject email address if there are less than 2 tokens on the right of @
if(emailRightPart.length < 2)
{
return false;
}
//check each right side token (domain, sub domain tokens etc) for valid characters
//reject email if a token contains invalid chars
var isTokenValid = false;
for(var i= 0; i < emailRightPart.length; i=i+1)
{
isTokenValid = validateTokenChars(emailRightPart[i]);
if(isTokenValid == false)
{
return false;
}
}
//check the left side token (username part of email address) for valid chars
isTokenValid = false;
isTokenValid = validateTokenChars(emailPart[0]);
if(isTokenValid == false) //reject email if token contains invalid chars
{
return false;
}
// at this point the email address is valid
return true;
} // end of validateEmailAddress()
//============================
//this function is called by validateEmailAddress()
function validateTokenChars(strToken)
{
strToken = strToken.toLowerCase();
isTokenOK = true
for(var i=0; i < strToken.length; i=i+1)
{
if(strToken.charAt(i) != "0" && strToken.charAt(i) != "1" && strToken.charAt(i) != "2" && strToken.charAt(i) != "3" && strToken.charAt(i) != "4" &&
strToken.charAt(i) != "5" && strToken.charAt(i) != "6" && strToken.charAt(i) != "7" && strToken.charAt(i) != "8" && strToken.charAt(i) != "9" &&
strToken.charAt(i) != "a" && strToken.charAt(i) != "b" && strToken.charAt(i) != "c" && strToken.charAt(i) != "d" && strToken.charAt(i) != "e" &&
strToken.charAt(i) != "f" && strToken.charAt(i) != "g" && strToken.charAt(i) != "h" && strToken.charAt(i) != "i" && strToken.charAt(i) != "j" &&
strToken.charAt(i) != "k" && strToken.charAt(i) != "l" && strToken.charAt(i) != "m" && strToken.charAt(i) != "n" && strToken.charAt(i) != "o" &&
strToken.charAt(i) != "p" && strToken.charAt(i) != "q" && strToken.charAt(i) != "r" && strToken.charAt(i) != "s" && strToken.charAt(i) != "t" &&
strToken.charAt(i) != "u" && strToken.charAt(i) != "v" && strToken.charAt(i) != "w" && strToken.charAt(i) != "x" && strToken.charAt(i) != "y" &&
strToken.charAt(i) != "z" && strToken.charAt(i) != "." && strToken.charAt(i) != "&" && strToken.charAt(i) != "*" && strToken.charAt(i) != "+" &&
strToken.charAt(i) != "-" && strToken.charAt(i) != "/" && strToken.charAt(i) != "=" && strToken.charAt(i) != "?" && strToken.charAt(i) != "^" &&
strToken.charAt(i) != "_" && strToken.charAt(i) != "{" && strToken.charAt(i) != "}" )
{
isTokenOK = false;
i = strToken.length; //jump out of FOR loop
}
}
return isTokenOK;
}
//====================================
[/CODE]
[CODE]function validateFormData(form)
{
//declare local variables
var invalidExpression = /[$\@\#%^&*()[]+_{}`~=|]/;
var validCheckFirst = form.firstName.value.search(invalidExpression);
var validCheckLast = form.lastName.value.search(invalidExpression);
//validate the first name
if(form.firstName.value=="")
{
alert("First Name is a required field, please enter your first name.");
form.firstName.focus();
return false;
}
//validate the last name
else if (form.lastName.value=="")
{
alert("Last Name is a required field, please enter your last name.");
form.lastName.focus();
return false;
}
//validate that illegal characters have not been entered into first name
else if (validCheckFirst != -1)
{
alert("One or more of the characters entered for First Name are not valid, please re-enter your First Name");
return false;
}
//validate that illegal characters have not been entered into last name
else if (validCheckLast != -1)
{
alert("One or more of the characters entered for Last Name are not valid, please re-enter your Last Name");
return false;
}
//verify whether the default string is in the comment box
else if (form.Comment.value=="Comment")
{
alert("A Comment is required, please enter a meaningful Comment");
form.Comment.focus();
return false;
}
//verify that the comment box is not empty
else if (form.Comment.value.replace(/[srn]/g,"")=="")
{
alert("A Comment is required, please enter a meaningful Comment");
form.Comment.focus();
return false;
}
/********Start Code Provided By: Tirna**********/
//reject email address if there is no @ or if it contains spaces
if(form.formal.value.indexOf("@") == -1 || form.formal.value.indexOf(" ") != -1)
{
alert("E-Mail information entered was invalid. Please enter a valid E-Mail address");
form.formal.focus();
return false;
}
//reject the email address if there is more than 1 @
var count = 0;
for(var i=0; i < form.formal.value.length; i=i+1)
{
if(form.formal.value.charAt(i) == "@")
{
count = count + 1;
}
if(count > 1)
{
alert("E-Mail information entered was invalid. Please enter a valid E-Mail address");
form.formal.focus();
return false;
}
}
//reject the email address if it starts or ends with a dot or an @
if(form.formal.value.substring(0,1) == "." || form.formal.value.substring(form.formal.value.length-1) == "." || form.formal.value.substring(0,1) == "@" || form.formal.value.substring(form.formal.value.length-1) == "@")
{
alert("E-Mail information entered was invalid. Please enter a valid E-Mail address");
form.formal.focus();
return false;
}
//reject the email address if there is a dot after the @
if(form.formal.value.charAt((form.formal.value.indexOf("@")+1)) == ".")
{
alert("E-Mail information entered was invalid. Please enter a valid E-Mail address");
form.formal.focus();
return false;
}
//now split the email address at the @ symbol
//emailPart[0] contains string to the left of @
//emailPart[1] contains string to the right of @
var emailPart = form.formal.value.split("@");
//reject email address if there are 2 dots in a row to the right of @
for(var i=0; i < emailPart[1].length-1; i=i+1)
{
if(emailPart[1].charAt(i) == "." && emailPart[1].charAt(i+1) == ".")
{
alert("E-Mail information entered was invalid. Please enter a valid E-Mail address");
form.formal.focus();
return false;
}
}
//split the string on the right of the @ using a dot delimeter
var emailRightPart = emailPart[1].split(".");
//reject email address if there are less than 2 tokens on the right of @
if(emailRightPart.length < 2)
{
alert("E-Mail information entered was invalid. Please enter a valid E-Mail address");
form.formal.focus();
return false;
}
//check each right side token (domain, sub domain tokens etc) for valid characters
//reject email if a token contains invalid chars
var isTokenValid = false;
for(var i= 0; i < emailRightPart.length; i=i+1)
{
isTokenValid = validateTokenChars(emailRightPart[i]);
if(isTokenValid == false)
{
alert("E-Mail information entered was invalid. Please enter a valid E-Mail address");
form.formal.focus();
return false;
}
}
//check the left side token (username part of email address) for valid chars
isTokenValid = false;
isTokenValid = validateTokenChars(emailPart[0]);
if(isTokenValid == false) //reject email if token contains invalid chars
{
alert("E-Mail information entered was invalid. Please enter a valid E-Mail address");
form.formal.focus();
return false;
}
//return showing the email address as valid
return true;
/********End Code Provided By: Tirna**********/
}[/CODE]
[CODE]
if(isDataValid) {
return true;
} else { //some form data was invalid
alert(msg);
return false;
}
[/CODE]
0.1.9 — BETA 6.16