betheball
09-04-2003, 10:40 PM
As you will see this is my first post. I have been fighting with setting up form validation for days and am hoping someone here can relieve my headache. The script below doesn't validate anything. The form simply submits even if all the fields are blank. My JScript experience is about one hour. Please forgive the lenght of the post as the code is quite lengthy. In my form tag I have placed this:
onSubmit="check(this)"
Here is what I placed in the HEAD, please point out my error(s)
<script Language="JavaScript" Type="text/javascript">
<!--Begin
function check(theForm){
if (theForm.UserName.value == "")
{
alert("Please enter a value for the \"User Name\" field.");
theForm.UserName.focus();
return (false);
}
if (theForm.UserName.value.length < 5)
{
alert("Please enter at least 5 characters in the \"User Name\" field.");
theForm.UserName.focus();
return (false);
}
if (theForm.UserName.value.length > 12)
{
alert("Please enter at most 12 characters in the \"User Name\" field.");
theForm.UserName.focus();
return (false);
}
if (theForm.Password.value == "")
{
alert("Please enter a value for the \"Password\" field.");
theForm.Password.focus();
return (false);
}
if (theForm.Password.value.length < 6)
{
alert("Please enter at least 6 characters in the \"Password\" field.");
theForm.Password.focus();
return (false);
}
if (theForm.Password.value.length > 12)
{
alert("Please enter at most 12 characters in the \"Password\" field.");
theForm.Password.focus();
return (false);
}
if (theForm.Password_Confirm.value == "")
{
alert("Please enter a value for the \"Confirm Password\" field.");
theForm.Password_Confirm.focus();
return (false);
}
if (theForm.Password_Confirm.value.length < 6)
{
alert("Please enter at least 6 characters in the \"Confirm Password\" field.");
theForm.Password_Confirm.focus();
return (false);
}
if (theForm.Password_Confirm.value.length > 12)
{
alert("Please enter at most 12 characters in the \"Confirm Password\" field.");
theForm.Password_Confirm.focus();
return (false);
}
if(/\s/.test(theForm.password.value))
{
alert("Sorry, spaces aren't allowed in the password.");
return false;
}
if(theForm.Password.value!=theForm.Password_Confirm.value)
{
alert("Passwords don't match");
return false;
}
if (theForm.Email.value == "")
{
alert("Please enter a value for the \"Email\" field.");
theForm.Email.focus();
return (false);
}
if (theForm.T13.value == "")
{
alert("Please enter a value for the \"Confirm Email\" field.");
theForm.T13.focus();
return (false);
}
if(theForm.Email.value!=theForm.T13.value)
{
alert("Passwords don't match");
return false;
}
if(/^\w+@\w+(\.\w{1,6}){0,1}$/i.test(theForm.Email.value)){
if(!/ ^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|c
oop|info|pro|museum)$/.test(theForm.Email.value.split('.')[1])){ //put on above line
alert("Your e-mail address must end in a well-known domain or two letter country.");
return false;
}
}else{
alert("Please put your e-mail in this format: you@yourdomain.ext");
return false;
}
if (theForm.FName.value == "")
{
alert("Please enter a value for the \"First Name\" field.");
theForm.FName.focus();
return (false);
}
if (theForm.LName.value == "")
{
alert("Please enter a value for the \"Last Name\" field.");
theForm.LName.focus();
return (false);
}
if (theForm.Address1.value == "")
{
alert("Please enter a value for the \"Address\" field.");
theForm.Address1.focus();
return (false);
}
if (theForm.State.selectedIndex == 0)
{
alert("The first \"State\" option is not a valid selection. Please choose one of the other options.");
theForm.state.focus();
return (false);
}
if (theForm.Country.selectedIndex == 0)
{
alert("The first \"Country\" option is not a valid selection. Please choose one of the other options.");
theForm.Country.focus();
return (false);
}
if (theForm.DOB1.selectedIndex == 0)
{
alert("The first \"Date of Birth - Month\" option is not a valid selection. Please choose one of the other options.");
theForm.DOB1.focus();
return (false);
}
if (theForm.DOB2.selectedIndex == 0)
{
alert("The first \"Date of Birth - Day\" option is not a valid selection. Please choose one of the other options.");
theForm.DOB2.focus();
return (false);
}
if (theForm.DOB3.selectedIndex == 0)
{
alert("The first \"Date of Birth - Year\" option is not a valid selection. Please choose one of the other options.");
theForm.DOB3.focus();
return (false);
}
var radioSelected = false;
for (i = 0; i < theForm.Gender.length; i++)
{
if (theForm.Gender[i].checked)
radioSelected = true;
}
if (!radioSelected)
{
alert("Please select one of the \"Gender\" options.");
return (false);
}
if (theForm.Time_Difference.selectedIndex == 0)
{
alert("The first \"Time Zone Difference\" option is not a valid selection. Please choose one of the other options.");
theForm.Time_Difference.focus();
return (false);
}
return true;
}
// End -->
</script>
onSubmit="check(this)"
Here is what I placed in the HEAD, please point out my error(s)
<script Language="JavaScript" Type="text/javascript">
<!--Begin
function check(theForm){
if (theForm.UserName.value == "")
{
alert("Please enter a value for the \"User Name\" field.");
theForm.UserName.focus();
return (false);
}
if (theForm.UserName.value.length < 5)
{
alert("Please enter at least 5 characters in the \"User Name\" field.");
theForm.UserName.focus();
return (false);
}
if (theForm.UserName.value.length > 12)
{
alert("Please enter at most 12 characters in the \"User Name\" field.");
theForm.UserName.focus();
return (false);
}
if (theForm.Password.value == "")
{
alert("Please enter a value for the \"Password\" field.");
theForm.Password.focus();
return (false);
}
if (theForm.Password.value.length < 6)
{
alert("Please enter at least 6 characters in the \"Password\" field.");
theForm.Password.focus();
return (false);
}
if (theForm.Password.value.length > 12)
{
alert("Please enter at most 12 characters in the \"Password\" field.");
theForm.Password.focus();
return (false);
}
if (theForm.Password_Confirm.value == "")
{
alert("Please enter a value for the \"Confirm Password\" field.");
theForm.Password_Confirm.focus();
return (false);
}
if (theForm.Password_Confirm.value.length < 6)
{
alert("Please enter at least 6 characters in the \"Confirm Password\" field.");
theForm.Password_Confirm.focus();
return (false);
}
if (theForm.Password_Confirm.value.length > 12)
{
alert("Please enter at most 12 characters in the \"Confirm Password\" field.");
theForm.Password_Confirm.focus();
return (false);
}
if(/\s/.test(theForm.password.value))
{
alert("Sorry, spaces aren't allowed in the password.");
return false;
}
if(theForm.Password.value!=theForm.Password_Confirm.value)
{
alert("Passwords don't match");
return false;
}
if (theForm.Email.value == "")
{
alert("Please enter a value for the \"Email\" field.");
theForm.Email.focus();
return (false);
}
if (theForm.T13.value == "")
{
alert("Please enter a value for the \"Confirm Email\" field.");
theForm.T13.focus();
return (false);
}
if(theForm.Email.value!=theForm.T13.value)
{
alert("Passwords don't match");
return false;
}
if(/^\w+@\w+(\.\w{1,6}){0,1}$/i.test(theForm.Email.value)){
if(!/ ^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|c
oop|info|pro|museum)$/.test(theForm.Email.value.split('.')[1])){ //put on above line
alert("Your e-mail address must end in a well-known domain or two letter country.");
return false;
}
}else{
alert("Please put your e-mail in this format: you@yourdomain.ext");
return false;
}
if (theForm.FName.value == "")
{
alert("Please enter a value for the \"First Name\" field.");
theForm.FName.focus();
return (false);
}
if (theForm.LName.value == "")
{
alert("Please enter a value for the \"Last Name\" field.");
theForm.LName.focus();
return (false);
}
if (theForm.Address1.value == "")
{
alert("Please enter a value for the \"Address\" field.");
theForm.Address1.focus();
return (false);
}
if (theForm.State.selectedIndex == 0)
{
alert("The first \"State\" option is not a valid selection. Please choose one of the other options.");
theForm.state.focus();
return (false);
}
if (theForm.Country.selectedIndex == 0)
{
alert("The first \"Country\" option is not a valid selection. Please choose one of the other options.");
theForm.Country.focus();
return (false);
}
if (theForm.DOB1.selectedIndex == 0)
{
alert("The first \"Date of Birth - Month\" option is not a valid selection. Please choose one of the other options.");
theForm.DOB1.focus();
return (false);
}
if (theForm.DOB2.selectedIndex == 0)
{
alert("The first \"Date of Birth - Day\" option is not a valid selection. Please choose one of the other options.");
theForm.DOB2.focus();
return (false);
}
if (theForm.DOB3.selectedIndex == 0)
{
alert("The first \"Date of Birth - Year\" option is not a valid selection. Please choose one of the other options.");
theForm.DOB3.focus();
return (false);
}
var radioSelected = false;
for (i = 0; i < theForm.Gender.length; i++)
{
if (theForm.Gender[i].checked)
radioSelected = true;
}
if (!radioSelected)
{
alert("Please select one of the \"Gender\" options.");
return (false);
}
if (theForm.Time_Difference.selectedIndex == 0)
{
alert("The first \"Time Zone Difference\" option is not a valid selection. Please choose one of the other options.");
theForm.Time_Difference.focus();
return (false);
}
return true;
}
// End -->
</script>