Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function to check the range of #s within form fields

Status
Not open for further replies.

svoltmer

Technical User
Aug 29, 2003
8
US
Hi. Could someone please look at a form page Iam working on. I am trying to call a function named "rangeCheck()" to make shure that all the values entered in all of the 8 textfields are between 1-5. If this is true I need it to call the function calculate() to add all the text fields together and put that value into a seperate text field called "totalpoints". Could someone point me in the right direction, using the general script I have started with? Here is the link to my page.
 
Just grind it out. Instead of trying to loop through the elements I would check each one by name. That way you can give specific feedback about errors.
Code:
function rangeCheck(){
   var allGood = true;

   if (document.forms[0].cat1.value > 5){
      window.alert('Please enter a value between 1 and 5 for Category I.');
      document.forms[0].cat1.focus();
      allGood = false;
   }

   if ( allGood && document.forms[0].cat2.value > 5){
      window.alert('Please enter a value between 1 and 5 for Category II.');
      document.forms[0].cat2.focus();
      allGood = false;
   }
   
   . . .
   
   if(allGood) {
      calculate();
   }
}
With the field names you have chosen you could put these if blocks into a loop by calculating the name of the field to test and using eval(). But that just becomes a lot of work and introduces more chance for mistakes. With 8 fields to check just copy the code with minor modifications for each field.
 
Avoiding loops is a bad idea if there are many operations of the same type.
Code:
function rangeCheck()
{
flag = false;  //error flag
frm = document.forms[0];  // make your statements shorter
errorbox = "";  // striòg that collects input fields with wrong input

for (n=0; n<9; n++)
  if (frm.elements[i].value < 1 || frm.elements[i].value > 5)
   {
    flag = true;
    errorbox += i + &quot;, &quot;;
   }

if (flag)
  alert('Please enter a value between 1 and 5 for each points earned category.\nThere are errors in the following fields: ' + errorbox)

else
  calculate();
}
This code will loop through all the necessary fields, collects the ones with error input, and display message where the errors are. If no errors found - calls calculate() function.
You can change the message so it show not the number of field but, for example, it's name:

Code:
errorbox += frm.elements[i].name + &quot;, &quot;;

 
Hello star,

&quot;Avoiding loops is a bad idea if there are many operations of the same type.&quot;

Well that is an idea that needs to be examined.

One might say &quot;operations repeated on items of the same type&quot; which is not quite the same thing.

And the usefulness of a loop might depend on how easy it is to refer to the items. For example, the items in the form elements collection. Even so you might need to watch out for those elements that are fields with values between 1 and 5, in this case elements[4] through elements[11].

Then too, what about the impact of changing requirements? Suppose we add more fields, maybe some hidden fields, at the top of the form? Have to adjust those loop parameters if you used a loop; but the code which refers to the fields by name is OK.

Not to mention readability. Who knows what form[0].element[5] is, but myRatingForm.cat2, no problem.

And is 5 blocks of if{} code &quot;many&quot;? Maybe 50 is &quot;many&quot;. Maybe 5000. The few extra bytes in this situation are not going to exhaust memory or clog a dial-up connection.

&quot;Papa don't preach.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top