By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,818 Members | 1,381 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,818 IT Pros & Developers. It's quick & easy.

Dynamic form validation with JavaScript

P: 1
Hi,

I have a query that displays each row in a table. Each row is a form and I want to validate with JavaScript each form. How can I dynamically create functions ? Here's my code that does not work..

Thanks

Gilles

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. <!--
  3. function noEntry2(form_name) {
  4. PER_DEB_01 = document.form_name.PER_DEB_01.value;
  5. PER_DEB_02 = document.form_name.PER_DEB_02.value;
  6. PER_DEB_03 = document.form_name.PER_DEB_03.value;
  7.  
  8. var PER_DEB = new Date(2008, 01, 01);
  9. var PER_DEB2 = new Date(document.form_name.PER_DEB_01.value, document.form_name.PER_DEB_02.value, document.form_name.PER_DEB_03.value);
  10.  
  11.  
  12. if ( (PER_DEB_01.length < 4) || (PER_DEB_02.length < 2) || (PER_DEB_03.length < 2) ) {
  13.     alert("bla bla bla");
  14.     document.form_name.PER_DEB_01.focus();
  15.     return false;
  16. }
  17. else if (PER_DEB2 < PER_DEB) {
  18.   alert("bla bla bla");
  19.   return false;
  20. }
  21.  
  22. }
  23.  
  24. function interval1(form_name) {
  25. var DT_DEB_02 = eval('document.'+form_name+'.PER_DEB_02.value');
  26.  
  27.     if (DT_DEB_02 < 1 || DT_DEB_02 > 12) {
  28.         alert("bla bla bla");
  29.         document.form_name.PER_DEB_02.value = ""; // NOT WORKING
  30.         return false;
  31.     }
  32.     else {
  33.         return true;
  34.     }    
  35. }
  36.  
  37. function interval2(form_name) {
  38. var DT_DEB_02 = eval('document.'+form_name+'.PER_DEB_03.value');
  39.  
  40.     if (DT_DEB_03 < 1 || DT_DEB_03 > 12) {
  41.         alert("bla bla bla");
  42.         document.form_name.PER_DEB_03.value = ""; // NOT WORKING
  43.         return false;
  44.     }
  45.     else {
  46.         return true;
  47.     }    
  48. }
  49.  
  50. //-->
  51. </script>
  52.  
  53.  
  54.  
  55.  
  56. <table width="98%" align="center" border="0" cellpadding="2" cellspacing="0">
  57.  
  58. <?php
  59. $ctr = 1;
  60. while ($recz3 = oci_fetch_array($z3)) {
  61. echo "
  62. <tr>
  63.     <form action='save.php' name='form".$ctr."' method='post' onSubmit='return noEntry2(form_name);'>
  64.     <td>".$recz3["MEDIA"]."</td>
  65.     <td>
  66.     <table border='0' cellspacing='0' cellpadding='0'>
  67.     <tr>
  68.         <td><input name='PER_DEB_01' value=".substr($recz3["PER_DEB"], 0, 4)." type='text' class='idDate' onKeyUp='return autoTab(this, 4, event);' size='4' maxlength='4' style='width: 40px;'></td>
  69.         <td>-</td>
  70.         <td><input name='PER_DEB_02' value=".substr($recz3["PER_DEB"], 5, 2)." type='text' class='idDate' onKeyUp='return autoTab(this, 2, event);' size='2' maxlength='2' style='width: 30px;'></td>
  71.         <td>-</td>
  72.         <td><input name='PER_DEB_03' value=".substr($recz3["PER_DEB"], 8, 2)." type='text' class='idDate' onKeyUp='return autoTab(this, 2, event);' size='2' maxlength='2' style='width: 30px;'></td>
  73.     </tr>
  74.     </table>
  75.     </td>
  76.     <td align='center'><input name='submit' type='image' value='save' src='images/icone_save_v4.gif'></td>
  77.     </form>
  78. </tr>
  79. ";
  80. $ctr++;
  81. }
  82. ?>
  83. </table>
  84.  
Dec 10 '08 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
First step is:
Expand|Select|Wrap|Line Numbers
  1. document.forms[form_name]..
which will use the form name passed to the function rather than the literal "form_name" which most likely does not exist.
Dec 11 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.