FAQ |
Kalender |
![]() |
#1 | ||
|
|||
Medlem
|
Jag har ett problem med koden. Ska validera formuläret men det är fel någonstans.
Kan någon se vad det är för fel? <script type="text/javascript"> function validate (form) { var returnValue = true; var name = form.txtName.value; if (name == "") { returnValue = false; alert("You must enter a name"); document.frmCompetition.txtName.focus(); } var email=form.txtEmail.value; var rxEmail =/^\w(\.?[\w-])*@\w(\.?[\w-])*\.[a-z]{2,6}(\.[a-z]{2})?$/i; if (!rxEmail.test(email)) { returnValue = false; alert("You must enter a valid email adress"); document.frmCompetition.txtEmail.focus(); } var radioChosen = false; var radioButtons = form.radAnswer; for (var i=0; i<radioButtons.length; i++) { if (radioButtons[i].checked) { radioButtons=true; } } if (radioChosen == false) { returnValue = false; alert("Du svarade inte på frågan"); } var tieBreaker=form.txtTieBreaker.value; if (tieBreaker=="") { returnValue = false; alert("You must enter an answer for the tie breaker"); document.frmCompetition.txtTieBreaker.focus(); } var tieBreakerWords = tieBreaker.split(/\s+/g); wordCount = tieBreakerWords.length; if (wordCount > 20) ; returnValue = false; alert("Du får inte svara med mer än 20 ord. Du skrev "+ wordCount + "ord."); document.frmCompetition.txtTieBreaker.focus(); } return returnValue; } </script> </head> <body> <form name="frmCompetition" action="competition.asp" method="post" onsubmit="return validate(this);"> <h2>An example Competition form (Tyvärr finns inga riktiga priser)</h2> To enter the drawing to win a case of Jenny´s Jam, first answer this questions: "What color are the strawberries" Then provide an answer for tie-breaker question: " I would like to win a case of Jennys Jam because ..." no more than 20 words.</p> <table> <tr> <td class="formTitle">Name: </td> <td><input type="text" name="txtName" size="18" /></td> </tr> <tr> <td class="formTitle">Email: </td> <td><input type="text" name="txtEmail" size="18" /></td> </tr> <tr> <td class="formTitle">Answer: </td> <td><input type="radio" name="radAnswer" value="Red" />Red <input type="radio" name="radAnswer" value="Gray" />Gray <input type="radio" name="radAnswer" value="Blue" />Blue</td> </tr> <tr> <td class="formTitle">Tiebreaker (no more than 20 words): </td> <td><textarea name="txtTieBreaker" cols="30" rows="3" /></textarea></td> </tr> <tr> <td class="formTitle"></td> <td><input type="submit" value="Enter now" /></td> </tr> </table> </form> |
||
![]() |
![]() |
![]() |
#2 | ||
|
|||
Medlem
|
Nästan i slutet av ditt javascript har du skrivit:
if (wordCount > 20) ; returnValue = false; alert("Du får inte svara med mer än 20 ord. Du skrev "+ wordCount + "ord."); document.frmCompetition.txtTieBreaker.focus(); } Men det bör nog vara: if (wordCount > 20) { returnValue = false; alert("Du får inte svara med mer än 20 ord. Du skrev "+ wordCount + "ord."); document.frmCompetition.txtTieBreaker.focus(); } Hoppas det löser ditt problem. |
||
![]() |
![]() |
![]() |
#3 | ||
|
|||
Medlem
|
Det gjorde det. tack för hjälpen.
|
||
![]() |
![]() |
Svara |
|
|