javascript form question

  • Pout
  • Born
  • Born
  • Pout
  • Posts: 2

Post 3+ Months Ago

Here'z the script i have atm:




Code: [ Select ]
<html>
<HEAD>

<script language="javascript">
<!--

function autotab(original,destination){
if (original.getAttribute&&original.value.length==original.getAttribute("maxlength"))
destination.focus()
}


function validateNum()
{
 

  // digits letters only
  if ( event.srcElement.value.search(/[^0-9]/) != -1 )
  {
   alert("only digits in this field");
   this.focus();
   return false;
  }
 
  return true;
  }
}

var bDone = false;

function getValue(o)
{
  o.value = document.inputform.result.value.substring(document.inputform.result.value.length - o.maxLength);
  document.inputform.result.value = document.inputform.result.value.substring(0,document.inputform.result.value.length - o.maxLength);
}

function back()
{
  if ( bDone ) return;
<!-- fill in the values in reverse order-->
  getValue(document.inputform.field3);
  getValue(document.inputform.field2);
  getValue(document.inputform.field1);
}

function next()
{
  if ( bDone ) return;
 

  if (
   !document.inputform.field1.onchange() ||
   !document.inputform.field2.onchange() ||
   !document.inputform.field3.onchange() ) return;
     

  document.inputform.result.value += document.inputform.field1.value + document.inputform.field2.value + document.inputform.field3.value;
  document.inputform.field1.value = '';
  document.inputform.field2.value = '';
  document.inputform.field3.value = '';
}

function done()
{
  bDone = true;
  document.getElementById('spResult').style.display = '';
}
-->
</script>

</HEAD>
<body>

  <form name=inputform>
   <table>
     <tr>
      <td>Field 1:</td>
      <td><input name=field1 type=text maxlength=1 onChange="java script:validateNum();" onKeyUp="autotab(this, document.inputform.field2)"></td>
     </tr>
     <tr>
      <td>Field 2:</td>
      <td><input name=field2 type=text maxlength=1 onChange="java script:validateNum();" onKeyUp="autotab(this, document.inputform.field3)"></td>
     </tr>
     <tr>
      <td>Field 3:</td>
      <td><input name=field3 type=text maxlength=1 onChange="java script:validateNum();" onKeyUp="autotab(this, document.inputform.nextbutton)"></td>
     </tr>
   </table>
     <input name="previousbutton" type="button" value="Previous" onClick="back();">
     <input name="nextbutton" type="button" value="Next" onClick="next();">
     <input name="donebutton" type="button" value="Done" onClick="done();">
   <span id=spResult style="display:none;"><input type=text name=result></span>
  </form>
</body>

</html>
  1. <html>
  2. <HEAD>
  3. <script language="javascript">
  4. <!--
  5. function autotab(original,destination){
  6. if (original.getAttribute&&original.value.length==original.getAttribute("maxlength"))
  7. destination.focus()
  8. }
  9. function validateNum()
  10. {
  11.  
  12.   // digits letters only
  13.   if ( event.srcElement.value.search(/[^0-9]/) != -1 )
  14.   {
  15.    alert("only digits in this field");
  16.    this.focus();
  17.    return false;
  18.   }
  19.  
  20.   return true;
  21.   }
  22. }
  23. var bDone = false;
  24. function getValue(o)
  25. {
  26.   o.value = document.inputform.result.value.substring(document.inputform.result.value.length - o.maxLength);
  27.   document.inputform.result.value = document.inputform.result.value.substring(0,document.inputform.result.value.length - o.maxLength);
  28. }
  29. function back()
  30. {
  31.   if ( bDone ) return;
  32. <!-- fill in the values in reverse order-->
  33.   getValue(document.inputform.field3);
  34.   getValue(document.inputform.field2);
  35.   getValue(document.inputform.field1);
  36. }
  37. function next()
  38. {
  39.   if ( bDone ) return;
  40.  
  41.   if (
  42.    !document.inputform.field1.onchange() ||
  43.    !document.inputform.field2.onchange() ||
  44.    !document.inputform.field3.onchange() ) return;
  45.      
  46.   document.inputform.result.value += document.inputform.field1.value + document.inputform.field2.value + document.inputform.field3.value;
  47.   document.inputform.field1.value = '';
  48.   document.inputform.field2.value = '';
  49.   document.inputform.field3.value = '';
  50. }
  51. function done()
  52. {
  53.   bDone = true;
  54.   document.getElementById('spResult').style.display = '';
  55. }
  56. -->
  57. </script>
  58. </HEAD>
  59. <body>
  60.   <form name=inputform>
  61.    <table>
  62.      <tr>
  63.       <td>Field 1:</td>
  64.       <td><input name=field1 type=text maxlength=1 onChange="java script:validateNum();" onKeyUp="autotab(this, document.inputform.field2)"></td>
  65.      </tr>
  66.      <tr>
  67.       <td>Field 2:</td>
  68.       <td><input name=field2 type=text maxlength=1 onChange="java script:validateNum();" onKeyUp="autotab(this, document.inputform.field3)"></td>
  69.      </tr>
  70.      <tr>
  71.       <td>Field 3:</td>
  72.       <td><input name=field3 type=text maxlength=1 onChange="java script:validateNum();" onKeyUp="autotab(this, document.inputform.nextbutton)"></td>
  73.      </tr>
  74.    </table>
  75.      <input name="previousbutton" type="button" value="Previous" onClick="back();">
  76.      <input name="nextbutton" type="button" value="Next" onClick="next();">
  77.      <input name="donebutton" type="button" value="Done" onClick="done();">
  78.    <span id=spResult style="display:none;"><input type=text name=result></span>
  79.   </form>
  80. </body>
  81. </html>



Now this gives me some problems:

When i filled in all the field correctly and hit the next button, it gives me the 'only digits in this field' alert. I don't know what causes that.

Second, when i click previous, all data inputed is supplied correctly, but let's say, when i click next again after 2 times previous it show all fields empty, if possible they should be filled in with the data (if there is any data that is)

Thirdly, i want to make it possible to override the autotab function when the user uses the Tab-key on the Keyboard so that adjustements can be made.

Hope u can help me on these matters

Thx in advance
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

The reason it's giving you the error when you click the next button has to do with the way the validateNum function is extracting the value to check it. When the Next button is clicked it is calling the onchange handler for the text boxes which in turn calls validateNum

It's using event.srcElement.value.search and it's the Next button that is generating the event in this case, so the value being checked is the value of the button which is just the string "Next" and that flunks the test.

I didn't have enough time to look at anyting else, but I'll try to look at it some more later. By the way you have an extra bracket in the validateNum function after return true and there shouldn't be any spaces in the word javascript inside the onchange handlers of the text boxes.
  • neronxeron
  • Born
  • Born
  • neronxeron
  • Posts: 4

Post 3+ Months Ago

you may want to check if all your function names match....but otherwise, follow RichB's instructions.

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 119 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.