Solved

validate text box-clause test

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

929 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

16 Experts available now in Live!

Get 1:1 Help Now