I've put together a little quiz using javascript. It doesn't actually keep score, it let's you keep answering the same question until you get it right. Most of the questions use multiple choice radio buttons and I have them working fine. One of the questions requires the user to enter a letter into 5 text input fields. The user has to enter A,B,C,D or E into each field. I wanted it to allow upper or lower case, so I used regular expressions to set a range of acceptable answers for each field. I can get it to work on one field but the way the function works, if they get the first answer right, it returns true and thinks it is finished and then takes the user to the next page without validating the other 4 fields. Here's the script:
Expand|Select|Wrap|Line Numbers
- <script language="javascript">
- <!--
- g1 = /[eE]/
- g2 = /[dD]/
- g3 = /[aA]/
- g4 = /[cC]/
- g5 = /[bB]/
- function checkanswer() {
- {
- if (g1.test(question4.goal1.value)) {
- return true
- }
- alert ("That is incorrect. Please try again.")
- question4.goal1.focus()
- question4.goal1.select()
- return false
- }