JasonDFR
11-04-2008, 01:15 PM
I have a form with dynamiclly created <input /> fields.
One type=hidden field.
An unknown number of type=text fields.
One type=submit
I need to first, validate that the user submitted value of the type=text fields is numeric, and second, validate that the total of the numeric values in each type=text field is 10 or less.
I've done the PHP for this, but I want to always use javascript as well where possible to validate the form before it is submitted.
The javascript below is my first try, and it is not working. Please point me in the right direction.
<!--
function validate_vote(form)
{
valid = true;
var theForm = document.getElementById("vote_chronicles");
var inputs = theForm.getElementsByTagName("input");
var vote_count = 0;
foreach( inputs ){
if(inputs[i].type == "text"){
vote_count = (vote_count + inputs[i].value);
}
}
if (vote_count > 10) {
alert("Total votes cannot total more than 10");
valid = false;
}
return valid;
}
//-->
And the PHP / HTML ( if you want to critique this code as well, please do)
<form id="vote_chronicles" action="vote_chronicles_action.php" method="post" onsubmit="return validate_vote(this);">
<?php
if ( isset($_GET['msg']) ) {
if ( $_GET['msg'] == 1 ) { echo '<p class="error">The total number of votes cannot exceed 10</p>';}
}
?>
<fieldset>
<input type="hidden" name="vote_chronicles" value="set" />
<?php
while ($row = mysql_fetch_assoc($r)) { ?>
<div class="vc_container">
<a href=""><img src="/medias/chronicles/<?php echo $row['vc_ sm_photo_name']; ?>" alt="<?php echo $row['theme_name']; ?>" /></a>
<p>Theme: <?php echo $row['theme_name']; ?></p>
<p>Submitted by: <?php echo $row['name']; ?></p>
<p><?php echo substr($row['vc_text'], 0, 250); ?>... <a href="/blog/?p=<?php echo $row['vc_blog_page']; ?>">(Read More)</a></p>
<label for="<?php echo $row['vc_id']; ?>">Number of Votes: <input class="vote_input" name="<?php echo $row['vc_id']; ?>" type="text" maxlength="2" /></label> <!-- vc_id for database -->
</div> <!-- end vote_chronicle_container -->
<?php } ?>
<input class="submit" type="submit" name="submit" value="Vote!" />
</fieldset>
</form>
Thanks a lot guys!
Jason
One type=hidden field.
An unknown number of type=text fields.
One type=submit
I need to first, validate that the user submitted value of the type=text fields is numeric, and second, validate that the total of the numeric values in each type=text field is 10 or less.
I've done the PHP for this, but I want to always use javascript as well where possible to validate the form before it is submitted.
The javascript below is my first try, and it is not working. Please point me in the right direction.
<!--
function validate_vote(form)
{
valid = true;
var theForm = document.getElementById("vote_chronicles");
var inputs = theForm.getElementsByTagName("input");
var vote_count = 0;
foreach( inputs ){
if(inputs[i].type == "text"){
vote_count = (vote_count + inputs[i].value);
}
}
if (vote_count > 10) {
alert("Total votes cannot total more than 10");
valid = false;
}
return valid;
}
//-->
And the PHP / HTML ( if you want to critique this code as well, please do)
<form id="vote_chronicles" action="vote_chronicles_action.php" method="post" onsubmit="return validate_vote(this);">
<?php
if ( isset($_GET['msg']) ) {
if ( $_GET['msg'] == 1 ) { echo '<p class="error">The total number of votes cannot exceed 10</p>';}
}
?>
<fieldset>
<input type="hidden" name="vote_chronicles" value="set" />
<?php
while ($row = mysql_fetch_assoc($r)) { ?>
<div class="vc_container">
<a href=""><img src="/medias/chronicles/<?php echo $row['vc_ sm_photo_name']; ?>" alt="<?php echo $row['theme_name']; ?>" /></a>
<p>Theme: <?php echo $row['theme_name']; ?></p>
<p>Submitted by: <?php echo $row['name']; ?></p>
<p><?php echo substr($row['vc_text'], 0, 250); ?>... <a href="/blog/?p=<?php echo $row['vc_blog_page']; ?>">(Read More)</a></p>
<label for="<?php echo $row['vc_id']; ?>">Number of Votes: <input class="vote_input" name="<?php echo $row['vc_id']; ?>" type="text" maxlength="2" /></label> <!-- vc_id for database -->
</div> <!-- end vote_chronicle_container -->
<?php } ?>
<input class="submit" type="submit" name="submit" value="Vote!" />
</fieldset>
</form>
Thanks a lot guys!
Jason