Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 421
  • Last Modified:

validate text box-clause test

Part of the code are below. I need to continue it so that when you click the check results the score is displayed and the percentage results also.


<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>feedback form</TITLE>
<script LANGUAGE="JavaScript">

 
 function checkresults()
 {
 if (document.form1.clause1.value=="quickly")
       {
       return true;
       }
 if (document.form1.street.value=="nothing")
       {
       return true;
       }
 if (document.form1.street.value=="assist")
       {
       return true;
       }
 if (document.form1.street.value=="coaching")
       {
       return true;
       }
 if (document.form1.street.value=="skill")
       {
       return true;
       }
       return false;
 }

function maskKeyPress(objEvent)
      {
        var iKeyCode;        
        iKeyCode = objEvent.keyCode;                  
        if(iKeyCode>=97 && iKeyCode<=122) return true;
        return false;
      }

</script>

<LINK HREF=format.css TYPE=text/css REL=stylesheet >
<STYLE>
H2 {text-align:left;
    font-size:15pt;
    font-weight:bold;}
H5 {font-family:Comic sans MS;
    font-size:13pt;}
H4 {color:red;}  
</STYLE>
</HEAD>

<BODY>

<FORM name=form1 >



<H4>Please do the clause test using the pool of words below</H4>


<H5>How <INPUT type=text NAME=clause1 size=10 onkeypress="return maskKeyPress(event)"> can you find out what is unusual about this paragraph? It looks so <br>
ordinary that you would think that nothing was wrong with it at all and, in fact,<INPUT type=text NAME=clause2 size=10 onkeypress="return maskKeyPress(event)"> <br>
is. But it is unusual. Why? If you study it and think about it you may find out, but I am not <br>
going to<INPUT type=text NAME=clause3 size=10 onkeypress="return maskKeyPress(event)"> you in any way. You must do it without <INPUT type=text NAME=clause4 size=10 onkeypress="return maskKeyPress(event)">.No doubt, if you<br>
work at it for long, you will finally find a solution. Who knows? Go to work and try your <br>
<INPUT type=text NAME=clause5 size=10 onkeypress="return maskKeyPress(event)">. Most of you will find it in about half an hour.<br></H5>

<H4> Pool: quickly, coaching, skill, nothing, assit </H4>


<P><input TYPE="button" value="Check Results" onClick=validateresults()> <INPUT type=reset> </P>
</FORM>



</BODY>
</HTML>
0
sharfaa
Asked:
sharfaa
  • 2
1 Solution
 
Michel PlungjanIT ExpertCommented:
WHat is the mask event supposed to be good for?

<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>feedback form</TITLE>
<script LANGUAGE="JavaScript">
answers = new Array(
"quickly",
"nothing",
"assist",
"coaching",
"skill"
)

 
function validateresults(theForm) {
  good=0;
  bad=0;
  for (i=0;i<answers.length;i++) {
    answer = theForm.elements["clause"+(i+1)].value.toLowerCase();
    if (answer != "") {
      if (answer == answers[i]) good ++
      else bad++
    }
  }
  if (bad>0) {
    alert('Good: '+good+'\nErrors:'+ bad)
    return false
  }
}

function maskKeyPress(objEvent)  {

  var iKeyCode;      
  iKeyCode = objEvent.keyCode;  
             
  if(iKeyCode>=97 && iKeyCode<=122) return true;
  return false;
}

</script>

<LINK HREF=format.css TYPE=text/css REL=stylesheet >
<STYLE>
H2 {text-align:left;
    font-size:15pt;
    font-weight:bold;}
H5 {font-family:Comic sans MS;
    font-size:13pt;}
H4 {color:red;}  
</STYLE>
</HEAD>

<BODY>

<FORM name=form1 >



<H4>Please do the clause test using the pool of words below</H4>


<H5>How <INPUT type=text NAME=clause1 size=10 onkeypress="return maskKeyPress(event)"> can you find out what is unusual about this paragraph? It looks so <br>
ordinary that you would think that nothing was wrong with it at all and, in fact,<INPUT type=text NAME=clause2 size=10 onkeypress="return maskKeyPress(event)"> <br>
is. But it is unusual. Why? If you study it and think about it you may find out, but I am not <br>
going to<INPUT type=text NAME=clause3 size=10 onkeypress="return maskKeyPress(event)"> you in any way. You must do it without <INPUT type=text NAME=clause4 size=10 onkeypress="return maskKeyPress(event)">.No doubt, if you<br>
work at it for long, you will finally find a solution. Who knows? Go to work and try your <br>
<INPUT type=text NAME=clause5 size=10 onkeypress="return maskKeyPress(event)">. Most of you will find it in about half an hour.<br></H5>

<H4> Pool: quickly, coaching, skill, nothing, assit </H4>


<P><input TYPE="button" value="Check Results" onClick="validateresults(this.form)"> <INPUT type=reset> </P>
</FORM>



</BODY>
</HTML>


0
 
Michel PlungjanIT ExpertCommented:
Sorry, you want to change

  if (bad>0) {
    alert('Good: '+good+'\nErrors:'+ bad)
    return false
  }
}

to

  alert('Good: '+good+'\nErrors:'+ bad)
  return bad==0
}


and possibly

<form...
<input TYPE="button" value="Check Results" onClick="validateresults(this.form)">
to

<form... onSubmit="return validateresults(this)
<input TYPE="button" value="Check Results" onClick="validateresults(this.form)">
<input TYPE="submit" value="Send Results">

0
 
sharfaaAuthor Commented:
Well thank you very much .I have added the percentage and with the mask event there is no need to put the toLowerCase() and any other character except lowercase letters that  can be entered
Here is the corrected code.

<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>feedback form</TITLE>
<script LANGUAGE="JavaScript">
answers = new Array(
"quickly",
"nothing",
"assist",
"coaching",
"skill"
)

 
function validateresults(theForm) {
  good=0;
  bad=0;
  for (i=0;i<answers.length;i++) {
    answer = theForm.elements["clause"+(i+1)].value;
    if (answer != "") {
      if (answer == answers[i]) good ++
      else bad++
    }
  }
  alert('No. of Good answers out of 5 = '+good+'\nYour Percentage= '+((good/5)*100)+'  %')
  return bad==0
}



function maskKeyPress(objEvent)  {

  var iKeyCode;      
  iKeyCode = objEvent.keyCode;  
             
  if(iKeyCode>=97 && iKeyCode<=122) return true;
  return false;
}

</script>

<LINK HREF=format.css TYPE=text/css REL=stylesheet >
<STYLE>
H2 {text-align:left;
    font-size:15pt;
    font-weight:bold;}
H5 {font-family:Comic sans MS;
    font-size:13pt;}
H4 {color:red;}  
</STYLE>
</HEAD>

<BODY>

<FORM name=form1 onSubmit="return validateresults(this)" >





<H4>Please do the clause test using the pool of words below</H4>
<H5>CAUTION: You need to fill all text boxes before you click on check results<BR>
and make sure caps lock is not on </H5>


<H5>How <INPUT type=text NAME=clause1 size=10 onkeypress="return maskKeyPress(event)"> can you find out what is unusual about this paragraph? It looks so <br>
ordinary that you would think that nothing was wrong with it at all and, in fact,<INPUT type=text NAME=clause2 size=10 onkeypress="return maskKeyPress(event)"> <br>
is. But it is unusual. Why? If you study it and think about it you may find out, but I am not <br>
going to<INPUT type=text NAME=clause3 size=10 onkeypress="return maskKeyPress(event)"> you in any way. You must do it without <INPUT type=text NAME=clause4 size=10 onkeypress="return maskKeyPress(event)">.No doubt, if you<br>
work at it for long, you will finally find a solution. Who knows? Go to work and try your <br>
<INPUT type=text NAME=clause5 size=10 onkeypress="return maskKeyPress(event)">. Most of you will find it in about half an hour.<br></H5>

<H4> Pool: quickly, coaching, skill, nothing, assit </H4>


<P><input TYPE="button" value="Check Results" onClick="validateresults(this.form)">
<input TYPE=reset>
</P>
</FORM>



</BODY>
</HTML>
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now