Perform control schedule

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • viki1967
    Contributor
    • Oct 2007
    • 263

    Perform control schedule

    Hi there.

    Hi have this script:

    Code:
     <script language="javascript" type="text/javascript">
    <!--
     
    var fieldNames = new Array("IRE", "ISI", "IMI", "REL", "REV", "MAN", "SOP", "GUF", "FOR", "VAS");
     
    function loaded()
    {
    
      var tm, q = location.href.indexOf("?tm=");
      if (q < 0 || (tm = location.href.substr(q + 4)).length <= 0) return;
     
    
      tm = tm.split(";");
     
      var frmObj = document.timeForm;
     
      var re = /^([a-z]+)-(([01]\d|2[0-3]):[0-5]\d)$/;
     
      for ( var n = 0 ; n < tm.length ; n++ )
        {
          var rt = tm[n].match(re);
          if (rt && typeof(frmObj[rt[1]]) != "undefined")
            {
    
              frmObj[rt[1]].value = rt[2];
            }
        }
    }
     
    function unloading()
    {
    
      if (window.opener == null) return;
     
      window.opener.popupWin = null; 
    }
     
    function updateTime(txtObj)
    {
      if (window.opener == null) return;
     
      var re = /^([01]\d|2[0-3]):[0-5]\d$/;
     
      if (txtObj.value.length >= 5 && !txtObj.value.match(re))
        {
          alert("Inserire l'orario nel formato 'hh:mm'.");
          txtObj.focus();
        }
     
      var frmObj = txtObj.form;
      var elems = frmObj.elements;
    
      var result = "";
      var destObj = window.opener.document.myform.impiego;
     
        {
          for ( var n = 0 ; n < elems.length ; n++ )
            {
    
              if (elems[n].type == "text" && elems[n].value.match(re))
                {
                  if (result.length) result += ";";
     
                  result += elems[n].name + "-" + elems[n].value;
                }
            }
     
          destObj.value= result;
        }
      else destObj.value= "--:--"; 
    }
     
    // -->
    </script>

    To perform control schedule.
    That is schedule no superior to 7 hours and 36 minutes.

    For example:

    1) In the field of the form "IRE" inserted 08:00... this value is superior to 7:36
    Its' wrong !!!!


    2) In the field of the form "IRE" inserted 04:00 and in the field of the form "ISI" inserted 04:00.... this value is superior to 7:36
    Its' wrong !!!!

    Help me please, regards
    viki1967
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    For each field, get the hours and minutes by splitting the input using split(). Use parseInt to convert into a number. Then add them up. If the hours are more than 7, then validation fails. If the hours equal 7, but the minutes exceed 36, again the validation fails. If you get stuck, post your attempt.

    Comment

    • viki1967
      Contributor
      • Oct 2007
      • 263

      #3
      Sorry I not understand... for example ?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        For example, let's say you have 'field' which contains the input:
        [code=javascript]var vals = field.value.spl it(":");
        var hours = parseInt(vals[0]);
        var mins = parseInt(vals[1]);
        // if this is in a loop, you could now add them to the total hours/mins:
        totalHours += hours;
        totalMins += mins;
        // validate outside the loop
        if (totalHours > 7) // error
        if ((totalHours == 7) && (totalMins > 36)) // error here too.
        [/code]

        Comment

        • viki1967
          Contributor
          • Oct 2007
          • 263

          #5
          Sorry Acoder... but how to integrate your code with code existing ?
          Many Thanks....

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Where are you making the validation? In updateTime()?

            Comment

            • viki1967
              Contributor
              • Oct 2007
              • 263

              #7
              Yes Acoder in UpdateTime()

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Just as you've used a loop in there, loop over the form elements in a similar manner:
                [code=javascript]var totalHours = 0;
                var totalMins = 0;
                for ( var n = 0 ; n < elems.length ; n++ )
                {
                if (elems[n].type == "text" && elems[n].value.match(re ))
                {
                var vals = elems[n].value.split(": ");
                var hours = parseInt(vals[0]);
                var mins = parseInt(vals[1]);
                totalHours += hours;
                totalMins += mins;
                }
                }
                // validate outside the loop
                if (totalHours > 7) // error
                if ((totalHours == 7) && (totalMins > 36)) // error here too.
                [/code]

                Comment

                • viki1967
                  Contributor
                  • Oct 2007
                  • 263

                  #9
                  Sorry Acoder, but not working....

                  [php]
                  function updateTime(txtO bj)
                  {
                  if (window.opener == null) return;


                  var re = /^([01]\d|2[0-3]):[0-5]\d$/;


                  if (txtObj.value.l ength >= 5 && !txtObj.value.m atch(re))
                  {
                  alert("Inserire l'orario nel formato 'hh:mm'.");
                  txtObj.focus();
                  }


                  var frmObj = txtObj.form;
                  var elems = frmObj.elements ;


                  var result = "";
                  var destObj = window.opener.d ocument.myform. impiego;


                  if (frmObj.IRE.val ue.match(re))

                  var totalHours = 0;
                  var totalMins = 0;

                  {
                  for ( var n = 0 ; n < elems.length ; n++ )
                  {


                  if (elems[n].type == "text" && elems[n].value.match(re ))


                  {
                  var vals = elems[n].value.split(": ");
                  var hours = parseInt(vals[0]);
                  var mins = parseInt(vals[1]);
                  totalHours += hours;
                  totalMins += mins;
                  }

                  {
                  if (result.length) result += ";";

                  result += elems[n].name + "-" + elems[n].value;
                  }
                  }

                  destObj.value= result;
                  }
                  else destObj.value= "--:--";

                  // validate outside the loop
                  if (totalHours > 7) // error
                  if ((totalHours == 7) && (totalMins > 36)) // error here too.

                  }
                  [/php]

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    There are two problems: one is that you want to validate before you calculate the result. So make two loops. The second problem is that where I've added comments, e.g. // error, you need to replace that with an error message.

                    Comment

                    • viki1967
                      Contributor
                      • Oct 2007
                      • 263

                      #11
                      What solution for this script ?

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        What changes have you made to the script?

                        Comment

                        • viki1967
                          Contributor
                          • Oct 2007
                          • 263

                          #13
                          [php]
                          function updateTime(txtO bj)
                          {
                          if (window.opener == null) return;


                          var re = /^([01]\d|2[0-3]):[0-5]\d$/;


                          if (txtObj.value.l ength >= 5 && !txtObj.value.m atch(re))
                          {
                          alert("Inserire l'orario nel formato 'hh:mm'.");
                          txtObj.focus();
                          }


                          var frmObj = txtObj.form;
                          var elems = frmObj.elements ;


                          var result = "";
                          var destObj = window.opener.d ocument.myform. impiego;


                          if (frmObj.IRE.val ue.match(re))

                          var totalHours = 0;
                          var totalMins = 0;

                          {
                          for ( var n = 0 ; n < elems.length ; n++ )
                          {


                          if (elems[n].type == "text" && elems[n].value.match(re ))


                          {
                          var vals = elems[n].value.split(": ");
                          var hours = parseInt(vals[0]);
                          var mins = parseInt(vals[1]);
                          totalHours += hours;
                          totalMins += mins;
                          }

                          {
                          if (result.length) result += ";";

                          result += elems[n].name + "-" + elems[n].value;
                          }
                          }

                          destObj.value= result;
                          }
                          else destObj.value= "--:--";

                          // validate outside the loop
                          if (totalHours > 7) // error
                          if ((totalHours == 7) && (totalMins > 36)) // error here too.

                          }

                          [/php]

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            I've made some quick changes without testing to give you an idea:
                            [code=javascript]function updateTime(txtO bj)
                            {
                            if (window.opener == null) return;

                            var re = /^([01]\d|2[0-3]):[0-5]\d$/;

                            if (txtObj.value.l ength >= 5 && !txtObj.value.m atch(re))
                            {
                            alert("Inserire l'orario nel formato 'hh:mm'.");
                            txtObj.focus();
                            }


                            var frmObj = txtObj.form;
                            var elems = frmObj.elements ;


                            var result = "";
                            var destObj = window.opener.d ocument.myform. impiego;

                            var totalHours = 0;
                            var totalMins = 0;

                            for ( var n = 0 ; n < elems.length ; n++ )
                            {


                            if (elems[n].type == "text" && elems[n].value.match(re ))
                            {
                            var vals = elems[n].value.split(": ");
                            var hours = parseInt(vals[0]);
                            var mins = parseInt(vals[1]);
                            totalHours += hours;
                            totalMins += mins;
                            }
                            }
                            // validate outside the loop
                            if (totalHours > 7) {
                            alert("Error: total hours is more than 7");
                            return;
                            }
                            if ((totalHours == 7) && (totalMins > 36)) {
                            alert("Error: total time exceeds 7:36");
                            return;
                            }
                            // validation complete...
                            for ( var n = 0 ; n < elems.length ; n++ )
                            {

                            if (elems[n].type == "text" && elems[n].value.match(re ))
                            {
                            {
                            if (result.length) result += ";";

                            result += elems[n].name + "-" + elems[n].value;
                            }
                            }

                            destObj.value= result;
                            }
                            }[/code]

                            Comment

                            • viki1967
                              Contributor
                              • Oct 2007
                              • 263

                              #15
                              Thanks Acoder.
                              But 09:00 value is validated by function...

                              Comment

                              Working...