Javascript validation of textfield boxes

I need to validate two out of three textfield boxes. Each textfield box has a unique name (FLNB, FLMB, and FLSB).
 
For example if a person enters information in textfield FLNB, than validation has to ensure that either FLMB or FLSB has information in the textfields as well.

Below is my current validation in Javascript for the current form.  Any suggestions or assistance with code is greatly appreciated. Thank you.

function Form1_Validator(theForm)
{


var alertsay = ""; 
// check to see if the field is blank

if (theForm.prefix.selectedIndex < 0)
{
alert("Please select your prefix in the \"Prefix\" options.");
theForm.prefix.focus();
return (false);
}

// check if the first drop down is selected, if so, invalid selection
if (theForm.prefix.selectedIndex == 0)
{
alert("The first \"Prefix\" option is not a valid selection.");
theForm.prefix.focus();
return (false);
}


if (theForm.fname.value == "")
{
alert("Please enter your first name in the \"First Name\" field");
theForm.fname.focus();
return (false);
}

if (theForm.fname.value.length < 2)
{
alert("Please enter at least 2 characters in the \"First Name\" field");
theForm.fname.focus();
return (false);
}


if (theForm.lname.value == "")
{
alert("Please enter your last name in the \"Last Name\" field");
theForm.lname.focus();
return (false);
}

if (theForm.lname.value.length < 2)
{
alert("Please enter at least 2 characters in the \"Last Name\" field");
theForm.lname.focus();
return (false);
}


if (theForm.wherefile.selectedIndex < 0)
{
alert("Please select where you predominantly file from the \"Predominantly File\" options.");
theForm.wherefile.focus();
return (false);
}

// check if the first drop down is selected, if so, invalid selection
if (theForm.wherefile.selectedIndex == 0)
{
alert("The first \"Predominantly File\" option is not a valid selection.");
theForm.wherefile.focus();
return (false);
}


if (theForm.street.value == "")
{
alert("Please enter your street address in the \"Street Address\" field");
theForm.street.focus();
return (false);
}

if (theForm.street.value.length < 8)
{
alert("Please enter at least 8 characters in the \"Address\" field");
theForm.street.focus();
return (false);
}


if (theForm.city.value == "")
{
alert("Please enter your city in the \"City\" field");
theForm.city.focus();
return (false);
}

if (theForm.city.value.length < 3)
{
alert("Please enter at least 3 characters in the \"City\" field");
theForm.city.focus();
return (false);
}


if (theForm.state.selectedIndex < 0)
{
alert("Please select one of the \"State\" options.");
theForm.state.focus();
return (false);
}

// check if the first drop down is selected, if so, invalid selection
if (theForm.state.selectedIndex == 0)
{
alert("The first \"State\" option is not a valid selection.");
theForm.state.focus();
return (false);
}


if (theForm.zip.value == "")
{
alert("Please enter your zip code in the \"Zip Code\" field");
theForm.zip.focus();
return (false);
}

if (theForm.zip.value.length < 5)
{
alert("Please enter at least 5 numeric characters in the \"Zip Code\" field");
theForm.zip.focus();
return (false);
}


if (theForm.phone.value == "")
{
alert("Please enter your telephone number in the \"Phone Number\" field");
theForm.phone.focus();
return (false);
}

if (theForm.phone.value.length < 10)
{
alert("Please enter at least 10 numeric characters in the \"Phone Number\" field");
theForm.phone.focus();
return (false);
}


if (theForm.emailadd.value == "")
{
alert("Please enter a valid email address for the \"Email Address\" field.");
theForm.emailadd.focus();
return (false);
}


// test if valid email address, must have @ and .
var checkEmail = "@.";
var checkStr = theForm.emailadd.value;
var EmailValid = false;
var EmailAt = false;
var EmailPeriod = false;
for (i = 0;  i < checkStr.length;  i++)
{
ch = checkStr.charAt(i);
for (j = 0;  j < checkEmail.length;  j++)
{
if (ch == checkEmail.charAt(j) && ch == "@")
EmailAt = true;
if (ch == checkEmail.charAt(j) && ch == ".")
EmailPeriod = true;
	  if (EmailAt && EmailPeriod)
		break;
	  if (j == checkEmail.length)
		break;
	}
	// if both the @ and . were in the string
if (EmailAt && EmailPeriod)
{
		EmailValid = true
		break;
	}
}
if (!EmailValid)
{
alert("The \"Email Address\" field must contain an \"@\" and a \".\"");
theForm.emailadd.focus();
return (false);
}


if (theForm.district.selectedIndex < 0)
{
alert("Please select where you are currently filing in the \"Currently Filing\" options.");
theForm.district.focus();
return (false);
}

// check if the first drop down is selected, if so, invalid selection
if (theForm.district.selectedIndex == 0)
{
alert("The first \"Currently Filing\" option is not a valid selection.");
theForm.district.focus();
return (false);
}


if (theForm.bar_num.value == "")
{
alert("Please enter your state bar number in the \"Bar Number\" field");
theForm.bar_num.focus();
return (false);
}

if (theForm.bar_num.value.length < 5)
{
alert("Please enter at least 5 characters in the \"Bar Number\" field");
theForm.bar_num.focus();
return (false);
}


return confirm("All validations have succeeded and the form is ready to be submitted. Click the OK button to finalize or Cancel to abort. Please DO NOT click the \"SUBMIT\" button again as errors may occur with your submission. Thank you.");
}

Open in new window

arendt73Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StingRaYCommented:
if (theForm.FLNB.value != "" && theForm.FLMB.value == "" && theForm.FLSB.value == "") 
{
alert("Please enter a valid FLMB field.");
theForm.FLMB.focus();
return (false);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
arendt73Author Commented:
StingRay, I attempted your suggestion and it works. One question, what happens if a user does not enter a selection in one of the three textfields? Right now, validation of the three texfields does not occur.

What can be added to ensure that the textfields have at least one entry to ensure the new validation you suggested works?

Thank you!
0
StingRaYCommented:
To check if AT LEAST on field contains information:
if (theForm.FLNB.value == "" && theForm.FLMB.value == "" && theForm.FLSB.value == "") 
{
alert("Please enter at least one of FLNB, FLMB or FLSB field.");
theForm.FLNB.focus();
return (false);
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.