...

View Full Version : Resolved: validate field



miknq
09-15-2005, 04:28 PM
Hello hello,

I was wondering if anyone could help me out with this?

A friend wrote me a nice little script to validate checkboxes for a quiz I was making.. problem on success and submit of the form, the email field isn't validated.

[CODE]

function validate(){

var questions = test.question; //Creates an array of elements from form "test" with the name "question"
cor_count = 0; //Counts the number of correct answers selected
for (var i=0; i<questions.length; i++) if (questions[i].value == "correct" && questions[i].checked) cor_count++;
if (cor_count==10)

{
alert("Success: submitting entry");
return true;
}
else alert("You selected " + cor_count + " correct species. You may try again.")
return false;
}

function adjust() {
questions = test.question; //Creates an array of elements from form "test" with the name "question"
sel_count = 0; //Counts the number of check boxes the user has selected
for (var i = 0; i<questions.length; i++) if (questions[i].checked) sel_count++;
if (sel_count==10) {
for (var i=0; i<questions.length; i++) if (!questions[i].checked) questions[i].disabled=true;
test.submit.disabled=false;
}
else {
for (var i=0; i<questions.length; i++) questions[i].disabled=false;
test.submit.disabled=true;
}

}

[CODE]

Obviously I have a form (just built in pain html) that has a 'email' text field whole bunch of checkboxes and a submit button.

If anyone could help me validate that email field that would be awesome.

Thanks

Canuck WebGeek
09-15-2005, 09:08 PM
Here's the regular expression I normally use for basic email validation:
/(^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$)/

miknq
09-16-2005, 02:37 AM
Hi Thanks for your reply...

Could you please tell me where exactly I should be placing this code.. i'm not much of a JS coder.. a friend wrote me the code I posted..

Should the email validation be placed inside the code I posted or should it be something separate?

Thanks very much

JUD
09-16-2005, 12:31 PM
You could put it in your if statement that checks if all the questions are right

Like this :


function validate(){

var questions = test.question; //Creates an array of elements from form "test" with the name "question"
cor_count = 0; //Counts the number of correct answers selected
for (var i=0; i<questions.length; i++) if (questions[i].value == "correct" && questions[i].checked) cor_count++;
var emailRegExp = /^[a-zA-Z0-9]+[\w\.-]*@[a-zA-Z0-9]+[\w\.-]*[a-zA-Z]+$/;
if (cor_count==10)

{
if(!emailRegExp.test(document.yourForm.yourEmailField.value)){
alert("Please enter a valid email address");
return false;
}
alert("Success: submitting entry");
return true;
}
else alert("You selected " + cor_count + " correct species. You may try again.")
return false;
}

miknq
09-19-2005, 04:04 AM
Hi thanks heaps for your reply.. very much appreciated..

However I still can't seam to get this working.. with the ammendments you gave me I have a page with the following..



<script language="JavaScript">
function validate(){

var questions = test.question; //Creates an array of elements from form "test" with the name "question"
cor_count = 0; //Counts the number of correct answers selected
for (var i=0; i<questions.length; i++) if (questions[i].value == "correct" && questions[i].checked) cor_count++;
var emailRegExp = /^[a-zA-Z0-9]+[\w\.-]*@[a-zA-Z0-9]+[\w\.-]*[a-zA-Z]+$/;
if (cor_count==10)

{
if(!emailRegExp.test(document.test.email.value)){
alert("Please enter a valid email address");
return false;
}
alert("Success: submitting entry");
return true;
}
else alert("You selected " + cor_count + " correct species. You may try again.")
return false;
}
</script>
<form name="test" action="/form/challengemail.php" method="post" target="_self" onsubmit="return validate()">
<table WIDTH="95%" border = "0" align = "center" cellpadding="1" cellspacing="1">
<tr>
<td WIDTH="5%"> <input type="checkbox" name="question" value="correct" onclick="adjust()">
<br> </td>
<td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Coronation
Trout</div></td>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect">
<br> </td>
<td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Dhufish</div></td>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect">
<br> </td>
<td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Coral
Cod</div></td>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect">
<br> </td>
<td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Marlin
Blue </div></td>
</tr>
<tr>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect">
<br> </td>
<td class="bodytxt"> <div align="left" class="bodytxt">Golden
Perch</div></td>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect">
<br> </td>
<td width="20%" class="bodytxt"> <div align="left">Barramundi</div></td>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="correct">
<br> </td>
<td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Mangrove
Jack</div></td>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="correct">
<br> </td>
<td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Spangled
Emperor </div></td>
</tr>
<tr>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect">
<br> </td>
<td class="bodytxt"> <div align="left" class="bodytxt">Saratoga</div></td>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="correct">
<br> </td>
<td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Albacore</div></td>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect">
<br> </td>
<td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Bonito</div></td>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect">
<br> </td>
<td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Sailfish</div></td>
</tr>
<tr>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="correct">
<br> </td>
<td class="bodytxt"> <div align="left" class="bodytxt">Spanish
Mackerel</div></td>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect">
<br> </td>
<td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Stripey</div></td>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="correct">
<br> </td>
<td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Blue
Groper</div></td>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="correct">
<br> </td>
<td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Catfish
Fork-tailed</div></td>
</tr>
<tr>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="correct">
<br> </td>
<td class="bodytxt"> <div align="left" class="bodytxt">John
Dory</div></td>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="correct">
<br> </td>
<td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Leatherjacket</div></td>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="incorrect">
<br> </td>
<td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Red
Bass</div></td>
<td WIDTH="5%"> <input name="question2" type="checkbox" id="question" onclick="adjust()" value="correct">
<br> </td>
<td width="20%" class="bodytxt"> <div align="left" class="bodytxt">Golden
Trevally</div></td>
</tr>
<tr>
<td COLSPAN="8" ALIGN="center"> <div align="right">&nbsp;&nbsp;&nbsp;<br>
<span class="bodytxt"> Email:</span>
<input name="email" type="text" id="email">
<input type="submit" name="submit" value="Submit">
&nbsp;&nbsp;&nbsp;<span class="bodytxt">

</span>&nbsp; </div></td>
</tr>
</table><script>adjust();</script>
</form>


Thanks very much if you can help me out any furthur..

Mik



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum