Solved

validate text box-clause test

Posted on 2003-11-05
3
414 Views
Last Modified: 2012-08-14
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
Comment
Question by:sharfaa
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 9685810
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9685823
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
 

Author Comment

by:sharfaa
ID: 9686010
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now