Solved

validate text box-clause test

Posted on 2003-11-05
3
416 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

786 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