JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 9th, 2004, 12:14 PM
squirto28 squirto28 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 2 squirto28 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Dynamic form validation

I am using php and I want to validate the form with javascript. The validation is that they must enter something and it must be numeric. The problem is that the text boxes are created dynamically from mysql database.

here is my php code for the text boxes:
PHP Code:
for ($i=0$i<$totalRows_Recordset1$i++)

 
$row_Recordset1 mysql_fetch_array($Recordset1);
 echo 
"<tr>";
 echo 
"<td>";
 echo 
$row_Recordset1[Item_Name];
 echo 
"</td>";
for (
$t=0$t <= 3$t++)

 echo 
"<td>"
 echo 
'<input name ="number_'.$i.$t.'" type="text" size="7">'
 echo 
"</td>"
}


I want the error message to display in a pop up window stating something like you did not fill in all the fields and/or you did not enter numeric data.

Thanks

Reply With Quote
  #2  
Old April 10th, 2004, 04:42 PM
klur's Avatar
klur klur is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 5 klur User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
possible answer

Hello!

you can try this:

when you create your input, add the following code <input onfocusout="check(this);" name ="number_'.$i.$t.'" type="text" size="7"> and be sure to include this funciton on the page, this function can be like this:

Code:
 
function check(obj) 
{
   if (obj == null) 
	   return; 
   if (obj.value == "" || isNaN(obj.value)) 
	   alert("does not work"); 
}


this function chech for the data in teh "value property" of the passed object, if this data is null or is not a number, show an alert, of course you can change this for your own code, this is only an idea;

thank you!

Reply With Quote
  #3  
Old April 16th, 2004, 01:08 PM
masood masood is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 8 masood User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Lightbulb

klur, good idea.

It may however cause a usability issue if someone gets multiple alert messages just from clicking in and out of the form fields (or using tab).

I'd suggest dynamically writing javascript to perform your validation:

1. Add code for the onSubmit event of the form tag: <FORM NAME="myFrm" onSubmit="return validate_form(this);" ... >

2. Inside of your for loop (php code), add a line that will store all input fields as an array:

Code:
 
<? 
$fldArCount = 0;
for ($i=0; $i<$totalRows_Recordset1; $i++)
{ 
$row_Recordset1 = mysql_fetch_array($Recordset1);
echo "<tr>";
echo "<td>";
echo $row_Recordset1[Item_Name];
echo "</td>";
for ($t=0; $t <= 3; $t++)
 { 
 $fldNameAr[$fldArCount] = 'number_'.$i.$t;
 echo "<td>"; 
 echo '<input name ="number_'.$i.$t.'" type="text" size="7">'; 
 echo "</td>"; 
 $fldArCount = $fldArCount + 1;
 }
} 
?>
</FORM>
<SCRIPT LANGUAGE="JAVASCRIPT">
// after the closing form tag, add the following:
function validate_form(fRef)
 {  
 <?
 for ($i = 0; $i < count($fldNameAr); $i++)
  {
  ?>
  if (fRef.<?=$fldNameAr[$i]?>.value == "")
   {
   alert("Please complete a value for <?=$fldNameAr[$i]?>");
   fRef.<?=$fldNameAr[$i]?>.focus();
   return false;
   } 
  <? 
  }
 ?>
 return true;
 }
</SCRIPT>


hope this helps

Reply With Quote
  #4  
Old April 16th, 2004, 01:39 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Get around the multiple alerts issue by using onchange() rather than the nonstandard onfocusout() (which I had never heard of -- is it a variant of onblur()?). Masood, your code doesn't seem to validate number values (the whole point of validation in this case). Klur's code is cleaner and more readily extended (add a "type" parameter and then have your logic switch on types to do different checks and you've got the beginning of a full-featured validation script).
__________________
Please don't PM me asking for solutions outside the scope of a thread.
Keeping all responses in a thread stands to help others who come along later,
which is after all what this forum's all about.

Reply With Quote
  #5  
Old April 16th, 2004, 02:02 PM
masood masood is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 8 masood User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sorry, missed the numeric validation requirement ( that's what I get for typing in the middle of a conf call).

onchange would be a good solution to the alert windows.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Dynamic form validation


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT