Web Analytics Made Easy -
StatCounter parseInt AND focus/select problem - CodingForum

Announcement

Collapse
No announcement yet.

parseInt AND focus/select problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • parseInt AND focus/select problem

    Hey people... me again ;-)

    I'm having a problem with, as my title says, parseInt and some focus or select (not sure which one goes, since none work)

    Here's part of my code - note that this is an assignment (ie, there would be easier ways to get to the result I need, but I have directions)
    HTML
    Code:
      <!---- WEEKDAYS & HOURS INPUT ---->
      <table width="700" frame=box rules=none border=5 name="hours_input" align="left" cellspacing=5 cellpadding=10>
        <tr><td colspan=7><I><center>Input the number of hours the employee has worked this week:</center></I></td></tr>
        <tr><th align=left width="15%">Monday</th>
            <th align=left width="15%">Tuesday</th>
            <th align=left width="15%">Wednesday</th>
            <th align=left width="15%">Thrusday</th>
            <th align=left width="15%">Friday</th>
            <th align=left width="15%">Saturday</th>
            <th align=left width="15%">Sunday</th></tr>
        <tr><td><center>
                 <input type="text" value="" name="day0" size=1 onChange="validateHours(this, 'Monday');">
                </center></td>
            <td><center>
                 <input type="text" value="" name="day1" size=1>
                </center></td>
            <td><center>
                 <input type="text" value="" name="day2" size=1>
                </center></td>
            <td><center>
                 <input type="text" value="" name="day3" size=1>
                </center></td>
            <td><center>
                 <input type="text" value="" name="day4" size=1>
                </center></td>
            <td><center>
                 <input type="text" value="" name="day5" size=1>
                </center></td>
            <td><center>
                 <input type="text" value="" name="day6" size=1>
                </center></td>
        </tr>
      </table>
    and here's the Javascript that goes with it. I have worked with only one day (just need to copy and paste when I got it right). By the way, the table above is in a form named EmpInfo
    Code:
    function validateHours(hour, day) {
    
      hr=hour.value;
    
      if (day =='Monday' || day =='Tueday' || day =='Wednesday' || day =='Thursday' || day =='Friday') {
        parseInt(hr);
        if (hr > 15 || isNan(hr)) {
          alert("You could not have worked " + hr + " hours on " + day);
          document.EmpInfo.hr=" ";
          document.EmpInfo.hr.select();
        }
      }
    }
    I get two major problems:
    * first, on the parseInt() line
    "Object expected" error
    * second, none of those line execute
    document.EmpInfo.hr=" ";
    document.EmpInfo.hr.select();
    and I get "object does not support this method or property" error

    I've played with this in all the ways I could think of to make it work, but I can't seem to figure it out. I have the same problem with a previous assignment, but was able to bypass it.
    Here, the error checking needs to be done in Javascript, and the processing of the data will be done in PHP
    Last edited by Lau; Feb 22, 2004, 04:20 PM.

  • #2
    try making an alert(hr) before the parseint, and btw ut

    parseint(hr,10);

    was told that if you omit the 10 you can't be entirely sure the hr is converted to a base 10 number.

    Comment


    • #3
      parseInt() isn't a mutator function - meaning - it doens't modify the value of the variable you pass in automatically. It performs its operation and returns the new value, which you must capture in a variable. You can use the same variable to store the new value, however.

      hr = parseInt( hr );
      My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
      “Minds are like parachutes. They don't work unless they are open”
      “Maturity is simply knowing when to not be immature”

      Comment


      • #4
        updated code

        Beetle, Garadon - I updated my code with your suggestion
        I also forgot to declare var h
        Code:
        function validateHours(hour, day) {
        
          var hr = hour.value;
          alert(hr);
          if (day =='Monday' || day =='Tueday' || day =='Wednesday' || day =='Thursday' || day =='Friday') {
            hr = parseInt(hr,10);
            if (hr > 15 || isNan(hr)) {
              alert("You could not have worked " + hr + " hours on " + day);
              document.EmpInfo.hr=" ";
              document.EmpInfo.hr.select();
            }
          }
        }
        well, it doesn't work. I still get the "object expected" error but I appreciate the help!

        Comment


        • #5
          try putting
          alert(hr) infront of parse int and tell us what it says

          Comment


          • #6
            Your misspelling of isNaN() is what was causing the "Object Expected" error. I made a few other tweaks.
            Code:
             <!---- WEEKDAYS & HOURS INPUT ---->
              <table width="700" frame=box rules=none border=5 name="hours_input" align="left" cellspacing=5 cellpadding=10>
                <tr><td colspan=7><I><center>Input the number of hours the employee has worked this week:</center></I></td></tr>
                <tr><th align=left width="15%">Monday</th>
                    <th align=left width="15%">Tuesday</th>
                    <th align=left width="15%">Wednesday</th>
                    <th align=left width="15%">Thrusday</th>
                    <th align=left width="15%">Friday</th>
                    <th align=left width="15%">Saturday</th>
                    <th align=left width="15%">Sunday</th></tr>
                <tr><td><center>
                         <input type="text" value="" name="day0" size=1 onChange="[COLOR=red][B]return [/B][/COLOR]validateHours(this, 'Monday');">
                        </center></td>
                    <td><center>
                         <input type="text" value="" name="day1" size=1>
                        </center></td>
                    <td><center>
                         <input type="text" value="" name="day2" size=1>
                        </center></td>
                    <td><center>
                         <input type="text" value="" name="day3" size=1>
                        </center></td>
                    <td><center>
                         <input type="text" value="" name="day4" size=1>
                        </center></td>
                    <td><center>
                         <input type="text" value="" name="day5" size=1>
                        </center></td>
                    <td><center>
                         <input type="text" value="" name="day6" size=1>
                        </center></td>
                </tr>
              </table>
              <script type="text/javascript">
              
            	function validateHours(hour, day)
            	{
            		var hr = hour.value;
            		if (day =='Monday' || day =='Tueday' || day =='Wednesday' || day =='Thursday' || day =='Friday')
            		{
            			hr = parseInt(hr,10);
            			if (hr > 15 || isNa[COLOR=red][B]N[/B][/COLOR](hr))
            			{
            				alert("You could not have worked " + hr + " hours on " + day);
            				[COLOR=red][B]hour.value[/B][/COLOR]=" ";
            				[COLOR=red][B]hour.[/B][/COLOR]select();
            				[COLOR=red][B]return false;[/B][/COLOR]
            			}
            		}
            		[COLOR=red][B]return true;[/B][/COLOR]
            	}
              
              </script>
            My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
            “Minds are like parachutes. They don't work unless they are open”
            “Maturity is simply knowing when to not be immature”

            Comment


            • #7
              YAY!!
              sweet Beetle... thank you so much
              I really didn't notice the spelling mistake, and even if I did, I probably wouldn't have though it was the problem!

              I noticed that the focus won't work without the little return statement. Can you explain me why?

              Garadon - tahnks for your input too! :-)

              Comment


              • #8
                Because all the actions in a function run by onchange occur before the event that triggers the change - in this case - tabbing or clicking away from the field. So, the script does actually focus on the correct input, but after the function exits, the actions that set it in motion complete.

                By returning false, we cancel the default effect of the event, allowing our script to do what we want it to.
                My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
                “Minds are like parachutes. They don't work unless they are open”
                “Maturity is simply knowing when to not be immature”

                Comment


                • #9
                  That makes a lot of sense! I'll keep that in mind
                  Thanks again
                  Last edited by Lau; Feb 22, 2004, 08:37 PM.

                  Comment

                  Working...
                  X