help with dynamic form validation

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
martincrumlish
Forum Newbie
Posts: 13
Joined: Mon Oct 14, 2002 10:46 am

help with dynamic form validation

Post by martincrumlish »

Hi,

I am writing a calculation program and I have a function to dynamically create the form fields. Each of these has a value with an $x added to the end which comes from the $x variable in the for loop.

What I need to do is validate these fields to make sure they are completed before submission.

It is easy enought of find a validation script for a form but i am having trouble with finding one that can handle dynamic fields. Ie: there could be 8 fields or there coulkd be 2....how can it make sure x amount of the required fields are completed?

here is the PHP function I am using to create the form. I would be happy with a PHP or JavaScript form validator if anyone can help.

Code: Select all

function print_row($bet,$number)
{
	echo "<table class=inputtable width=350 cellpadding=0>\n";
	echo "<tr>\n<td align=left colspan=6>\n<b>$bet</b>\n</td>\n";
	echo "</tr></table>";
	echo "<table class=inputtable width=335>";
	echo "<tr>";
	echo "<td align=left class=title>\n<b>Bet</b>\n</td>\n";
	echo "<td align=left class=title width=15>\n<b>Odds</b>\n</td>\n";
	echo "<td align=left class=deadheat>\n<b>D.H.<font size=1>*</font></b>\n</td>\n";
	echo "<td class=rulefour><b>R/4*</b></td>";
	echo "<td class=eachway><b>E/W*</b></td>";
	echo "<td class=status><b>W/L*</b></td>";
	echo "</tr>";
for( $x = 1; $x < $number; $x++ ) {
	echo "<tr>\n";
	echo "<td align=left class=oddstext width=15>\n$x:</td>\n";
	echo "<td align=left class=oddsform>";
	echo "<INPUT TYPE=text class=input1 NAME=odds$x SIZE=2> / <INPUT TYPE=text class=input1 NAME=odds$x"."a SIZE=1>";
	echo "</td>";
	echo "<td class=deadheatform>";
	echo "<select name=deadheat$x>\n<option></option>\n<option value=2>2</option>\n<option value=3>3</option>\n<option value=4>4</option>\n<option value=5>5</option>\n<option value=6>6</option>\n</select>";
	echo "</td>\n";
	echo "<td align=left class=rulefourform>\n<INPUT TYPE=text class=input1 NAME=rf$x SIZE=2>p</td>";
	echo "<td class=eachwayform><select name=eachway$x>\n<option></option>\n<option value=2>1/2</option>\n<option value=3>1/3</option>\n<option value=4>1/4</option>\n<option value=5>1/5</option>\n<option value=6>1/6</option>\n</select></td>";
	echo "<td class=statusform><select name=status$x>\n<option value=win>win</option>\n<option value=lose>lose</option>\n</select></td>";
	echo "</tr>\n";
	//echo "b:".$rf1." - ".$odds1." - ".$odds1a." - ".$stake;
}
	echo "</table>";
echo "<table class=inputtable width=150>";
echo "<tr>\n<td align=left class=staketext>\nStake:</td>\n<td align=left class=stakeform>\n<INPUT TYPE=text class=input1 NAME=stake SIZE=4>\n</td>\n</tr>\n";
echo "<tr>\n<td align=left colspan=2>\n<INPUT TYPE=submit NAME=submit VALUE=calculate>\n<br>\n<font size=1>(* D.H. = Dead Heat, R.4 = Rule 4, E/W = Each way, W/L = Win/Lose)</font></td>\n</tr>\n</table>\n";
}
$bet and $number or specified on a prvious page and $number dictates how many times the for loop runs and hence, how many rows with form fields there are.

Thanks in advance,

Martin
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

all fields text boxes

Post by phpScott »

Are all the fields you want to test going to be html text boxes?
If so give me a couple of hours and I have the exact javascript at my office that will work wonderfully for it.

Who know one of these might php guru's will probably having something by then.

phpScott
DeGauss
Forum Contributor
Posts: 105
Joined: Tue Oct 22, 2002 9:44 am
Location: Gainesville, FL

Post by DeGauss »

You're always going to know how many form fields you're going to have thanks to $x iterating through your form generation...

So after you've generated all the forms you need, you can then do this:

print "<input type=\"hidden\" name=\"numOfForms\" value=\"".$x."\">";

Then on the validation side of the script, you should be able to do the following...

for ($x=0;$x<=$_POST["numOfForms"];$x++) {
if (!$form[$x]) {
print "Hey Einstein! You forgot to enter a value for Form ".$x."!";
}
}

Does this help?[/img]
martincrumlish
Forum Newbie
Posts: 13
Joined: Mon Oct 14, 2002 10:46 am

Post by martincrumlish »

thanks phpScott.

Yes, they are all text boxes...however, i dont need to validate every text box, just: odds1, odds1a, odds2, odds2a.....odds5, odds5a etc. and stake
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

here you go

Post by phpScott »

I created to possible scripts for you. I know the first one works but it sounds like it might be a little bit of a over kill for what you need.
put this inside your head tag in your html
This script will check every text box in your first form and see if it is blank.
<script language="javascript">

function validate()
{
var form=window.document.forms[0];
for( var 1=0; i<form.elements.length; i++)
{
if(form.elements.type="text")
{
if(form.elements.value=="")
{
form.elements.focus();
return false;
}
}
}
}

</script>

This second one is a little more modified to do what I think it is your looking for. I haven't tested it yet but the syntax should be right. You might have to play with the big if statement to get it to go. If you can't get it to work let me know and I will spend some time testing and tweaking.

<script language="javascript">

function validate()
{
var form=window.document.forms[0];
for( var 1=0; i<form.elements.length; i++)
{
if(form.elements.name == "odds"+i || form.elements.name == "odds"+1+"a" || form.elements.name == "stake")
{
if(form.elements.value == "")
{
form.elements.focus();
return false;
}
}
}
}

</script>

phpScott
Post Reply