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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form validation

Status
Not open for further replies.
Nov 6, 2002
3
US
hey i need help with my form I'm making a test using form in and i want to give 1 pt for every anser that is correct and state it on the side of the text box. I was also wondering if I put the answer on the value ="" will that mean if the text type in the box is not the same as the value then the answer will be wrong? This is what i got so far:

function validate_form ( )
{
valid = true;

if ( document.test.aa.value= "pixel" )
{
alert ( "Please correctly answer question 1." );
valid = false;
}
if ( document.test.ab.value == "" )
{
alert ( "Please fill in the 'question 2' box." );
valid = false;
}
if ( document.test.ac.value == "" )
{
alert ( "Please fill in the 'question 3' box." );
valid = false;
}
if ( document.test.ad.value == "" )
{
alert ( "Please fill in the 'question 4' box." );
valid = false;
}
if ( document.test.ae.value == "" )
{
alert ( "Please fill in the 'question 5' box." );
valid = false;
}
if ( document.test.af.value == "" )
{
alert ( "Please fill in the 'question 6' box." );
valid = false;
}
if ( document.test.ag.value == "" )
{
alert ( "Please fill in the 'question 7' box." );
valid = false;
}
if ( document.test.ah.value == "" )
{
alert ( "Please fill in the '8' box." );
valid = false;
}
return valid;
}

 
Is this what you are trying to do????
Code:
function validate_form ( )
{
    valid = true;
    numRight = 0;
    alertString = "";

        if (document.test.aa.value != "pixel" )
        {
                alertStr += "\nQuestion #1"
        }
        else{
        	numRight += 1
        }
if ( document.test.ab.value == "answer2" )
        {
                alertStr += "\nQuestion #2"
        }
        else{
        	numRight += 1
        }
if ( document.test.ac.value == "answer3" )
        {
                alertStr += "\nQuestion #3"
        }
        else{
        	numRight += 1
        }
if ( document.test.ad.value == "answer4" )
        {
               alertStr += "\nQuestion #4"
        }
        else{
        	numRight += 1
        }
if ( document.test.ae.value == "answer5" )
        {
                alertStr += "\nQuestion #5"
        }
        else{
        	numRight += 1
        }
if ( document.test.af.value == "answer6" )
        {
                alertStr += "\nQuestion #6"
        }
        else{
        	numRight += 1
        }
if ( document.test.ag.value == "answer7" )
        {
                alertStr += "\nQuestion #7"
        }
        else{
        	numRight += 1
        }
if ( document.test.ah.value == "answer8" )
        {
                alertStr += "\nQuestion #8"
        }
        else{
        	numRight += 1
        }
if (alertStr.length > 0){
 		alert ("You have incorrectly answered the following questions:"+alertStr)
	}
document.test.numRight.value = numRight
document.test.numRight.readonly = true
}
 
I'd rather have used some easier way to think, like this:

function validate_frm() {
pass=0
if ((document.test.a1.value=="")||(document.test.a2.value=="") {
alert ("Fill the fields.")
pass ++
}
if (pass==0) {
document.test.submit()
}
}
</script>

and so on. Of course you want something more complex, but using this variable++ thing can be great
 
sorry, the function should be:

function validate_frm() {
pass==0
if ((document.test.a1.value==&quot;&quot;)||(document.test.a2.value==&quot;&quot;)) {
alert (&quot;Fill in the fields.&quot;)
pass ++
}
if (pass==0) {
document.test.submit()
}
}
</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top