[PHP] For your reference, how to validate dynamic form fields.

2004-04-06 Thread Hawkes, Richard
Just spent far too long trying to figure out how to do this, so I thought I'd
pass it on to you great guys, in case you need it for the future.
 
I used PHP to create multiple form rows, each one numbered:

?
for ($i =1; $i = 20; $i++) {
echo(input type=text name=userId$i maxlength=5/br/);
}
?

This creates a number of rows, each with a unique form value name. The problem
I had was that I needed to validate those rows, to make sure they were
numeric... Here's what I did. Have fun!
 
?php
$numRows = 20;
?
html
head
titleUser ID Entry Example/title
script language=JavaScript
function isNumeric(strString)
{
var strValidChars = 0123456789-.;
var strChar;
var blnResult = true;
 
if (strString.length == 0) return false;
 
//  test strString consists of valid characters listed above
for (i = 0; i  strString.length  blnResult == true; i++)
{
strChar = strString.charAt(i);
if (strValidChars.indexOf(strChar) == -1)
{
blnResult = false;
}
}
return blnResult;
}
 
 
function validateForm()
{
for (var i = 1; i = ?= $numRows ?; i++)
{
var fieldName = 'UserId' + i;
var fieldValue =
document.forms.userForm.elements[fieldName].value;
if (fieldValue != ''  ! isNumeric(fieldValue))
{
alert(Field value in row  + i +  ( + fieldValue + ) is
not numeric.);
return false;
}
}
return true;
}
/script
/head
body
form name=userForm action=process_form.php method=post onSubmit=return
validateForm()
?php
  for ($i =1; $i = $numRows; $i++) {
echo(input type=text name=userId$i maxlength=5/br/);
  }
?
/form
/body
/html

==
This message is for the sole use of the intended recipient. If you received
this message in error please delete it and notify us. If this message was
misdirected, CSFB does not waive any confidentiality or privilege. CSFB
retains and monitors electronic communications sent through its network.
Instructions transmitted over this system are not binding on CSFB until they
are confirmed by us. Message transmission is not guaranteed to be secure.
==


Re: [PHP] For your reference, how to validate dynamic form fields.

2004-04-06 Thread holmes072000
 From: Hawkes, Richard [EMAIL PROTECTED]
 
 Just spent far too long trying to figure out how to do this, so I thought I'd
 pass it on to you great guys, in case you need it for the future.
  
 I used PHP to create multiple form rows, each one numbered:
 
 ?
 for ($i =1; $i = 20; $i++) {
 echo(input type=text name=userId$i maxlength=5/br/);
 }
 ?

Use arrays. It'll make your life easier.

echo input type=\text\ name=\userId[$i]\ maxlength=\5\/br /);

You can still reference the element in Javascript, just by name or whatever you call 
it. 

form.element['userId[1]'] (or something like that).

You show a Javascript validation. Remember that this is easily bypassed so you also 
need to validate the numbers on the PHP side. 

?php
foreach($_POST['inputId'] as $id)
{
  if(!is_numeric($id))
  { echo $id is invalid!; }
}
?

---John Holmes...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php