Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation Problem

Status
Not open for further replies.

ThomasE

Technical User
Nov 20, 2002
12
0
0
AU
I am using the validation script below as part of a larger script for a sales form. At the bottom of the sales page I have a button that runs a function to open a printable page. I need to validate the form fields on the form before proceeding to the printable page. If I leave one of the fields blank...say the email address...it gives me the correct alert box telling me to fill in the email address, but after I close the alert box it goes brings up a blank page with 'False' written at the top. Can you tell me how I can get the focus to go back to the field that failed the validation???

Thanks


function formCheck()
{
if (document.Sales.email.value.indexOf("@") == -1 ||
document.Sales.email.value == "")
{
alert("Please include a proper email address.");
return false;
}

if (document.Sales.user_name.value == "")
{
alert("Please put in a name.");
return false;
}
}
 
well i think i can help you with focussing the one thats wrong...im just not sure about the other thing
Code:
function formCheck() 
    {
        if (document.Sales.email.value.indexOf("@") == -1 ||
            document.Sales.email.value == "") 
        {
        alert("Please include a proper email address.");
Code:
document.Sales.email.focus();
Code:
        return false;
        }
       
        if (document.Sales.user_name.value == "") 
        {
        alert("Please put in a name.");
Code:
document.Sales.user_name.focus();
Code:
        return false;
        }
    }

hope that helps "Those who dare to fail miserably can achieve greatly." - Robert F. Kennedy
So true..
 
Hey thanks Dookie2k2, I tried your code and the only thing I had to change was to place the 'document.Sales.user_name.focus();' under the 'return false;' line............now when I close the alert box the cursor is focused back onto the field that is not correctly filled in.

Thanks again
 
Oh just one quick question.......can you tell me what the code would be if I want to validate an option box using the method above??

Thanks
 
you can check to see if they never changed the value of the select box(named select1 in a form called form1..like this:
Code:
if (document.signup.select1.selectedIndex == 0)
	{
	alert ('Please select an option');
	return false;
	document.signup.select1.focus();
	}

hope that helps ya "Those who dare to fail miserably can achieve greatly." - Robert F. Kennedy
So true..
 
What if this all works great but.... I want it to submit the form - if ALL the form fields etc are filled in. Can it do a complete check, and if OK then "Submit"? Obviously if not, the alert shows them and where. • idaryl
idface.gif
 
here..this script I made checks all the form elements and checks for their respective values...and then if they all are ok, it submits the form...this should go in the head..and call it like this : <input type='button' onClick='checkAllValues()' value='Submit'> :
Code:
<script language='javascript'><!--
function checkAllValues()
{
var type;
var forms = document.forms;
for (l=0;l<forms.length;l++){
var elements = document.forms[0].elements;
for (x=0;x<elements.length;x++)
	{
	 type = elements[x].type;
	 if (type == 'text' || type == 'file' || type == 'password' || type == 'textarea')
		{
		if (elements[x].value == '')
			{alert ('You forgot to fill a field in..');
		        elements[x].focus();
			return false;
			  }
		}
	 if (type == 'checkbox')
		{
		if (elements[x].checked == false)
			{alert ('You forgot to check a field...');
		         elements[x].focus();
			 return false;
	
			 }
		}
	 if (type == 'radio')
		{
		name = elements[x].name;
		thinger = eval('document.forms[0].'+name);
		if (thinger[0].checked == false && thinger[1].checked == false)
		{
		alert ('Please select a radio button');
		elements[x].focus();	
		return false;
		}
		}
	 if (type == 'select-one')
		{
		if (elements[x].selectedIndex == '0')
		{alert ('Please select a drop-down choice');
		 elements[x].focus();		
		 return false;
		}
		}
	}
document.forms[l].submit();
}
}
//--></script>

hope this helps! &quot;Those who dare to fail miserably can achieve greatly.&quot; - Robert F. Kennedy
So true..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top