469,948 Members | 2,850 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,948 developers. It's quick & easy.

Perform control schedule

263 100+
Hi there.

Hi have this script:

Expand|Select|Wrap|Line Numbers
  1.  <script language="javascript" type="text/javascript">
  2. <!--
  3.  
  4. var fieldNames = new Array("IRE", "ISI", "IMI", "REL", "REV", "MAN", "SOP", "GUF", "FOR", "VAS");
  5.  
  6. function loaded()
  7. {
  8.  
  9.   var tm, q = location.href.indexOf("?tm=");
  10.   if (q < 0 || (tm = location.href.substr(q + 4)).length <= 0) return;
  11.  
  12.  
  13.   tm = tm.split(";");
  14.  
  15.   var frmObj = document.timeForm;
  16.  
  17.   var re = /^([a-z]+)-(([01]\d|2[0-3]):[0-5]\d)$/;
  18.  
  19.   for ( var n = 0 ; n < tm.length ; n++ )
  20.     {
  21.       var rt = tm[n].match(re);
  22.       if (rt && typeof(frmObj[rt[1]]) != "undefined")
  23.         {
  24.  
  25.           frmObj[rt[1]].value = rt[2];
  26.         }
  27.     }
  28. }
  29.  
  30. function unloading()
  31. {
  32.  
  33.   if (window.opener == null) return;
  34.  
  35.   window.opener.popupWin = null; 
  36. }
  37.  
  38. function updateTime(txtObj)
  39. {
  40.   if (window.opener == null) return;
  41.  
  42.   var re = /^([01]\d|2[0-3]):[0-5]\d$/;
  43.  
  44.   if (txtObj.value.length >= 5 && !txtObj.value.match(re))
  45.     {
  46.       alert("Inserire l'orario nel formato 'hh:mm'.");
  47.       txtObj.focus();
  48.     }
  49.  
  50.   var frmObj = txtObj.form;
  51.   var elems = frmObj.elements;
  52.  
  53.   var result = "";
  54.   var destObj = window.opener.document.myform.impiego;
  55.  
  56.     {
  57.       for ( var n = 0 ; n < elems.length ; n++ )
  58.         {
  59.  
  60.           if (elems[n].type == "text" && elems[n].value.match(re))
  61.             {
  62.               if (result.length) result += ";";
  63.  
  64.               result += elems[n].name + "-" + elems[n].value;
  65.             }
  66.         }
  67.  
  68.       destObj.value= result;
  69.     }
  70.   else destObj.value= "--:--"; 
  71. }
  72.  
  73. // -->
  74. </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
Feb 13 '08 #1
19 2092
acoder
16,027 Expert Mod 8TB
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.
Feb 14 '08 #2
viki1967
263 100+
Sorry I not understand... for example ?
Feb 14 '08 #3
acoder
16,027 Expert Mod 8TB
For example, let's say you have 'field' which contains the input:
Expand|Select|Wrap|Line Numbers
  1. var vals = field.value.split(":");
  2. var hours = parseInt(vals[0]);
  3. var mins = parseInt(vals[1]);
  4. // if this is in a loop, you could now add them to the total hours/mins:
  5. totalHours += hours;
  6. totalMins += mins;
  7. // validate outside the loop
  8. if (totalHours > 7) // error
  9. if ((totalHours == 7) && (totalMins > 36)) // error here too.
  10.  
Feb 14 '08 #4
viki1967
263 100+
Sorry Acoder... but how to integrate your code with code existing ?
Many Thanks....
Feb 14 '08 #5
acoder
16,027 Expert Mod 8TB
Where are you making the validation? In updateTime()?
Feb 14 '08 #6
viki1967
263 100+
Yes Acoder in UpdateTime()
Feb 14 '08 #7
acoder
16,027 Expert Mod 8TB
Just as you've used a loop in there, loop over the form elements in a similar manner:
Expand|Select|Wrap|Line Numbers
  1. var totalHours = 0;
  2. var totalMins = 0;
  3. for ( var n = 0 ; n < elems.length ; n++ )
  4.   {
  5.      if (elems[n].type == "text" && elems[n].value.match(re))
  6.         {
  7.            var vals = elems[n].value.split(":");
  8.            var hours = parseInt(vals[0]);
  9.            var mins = parseInt(vals[1]);
  10.            totalHours += hours;
  11.            totalMins += mins;
  12.         }
  13.   }
  14. // validate outside the loop
  15. if (totalHours > 7) // error
  16. if ((totalHours == 7) && (totalMins > 36)) // error here too.
  17.  
Feb 14 '08 #8
viki1967
263 100+
Sorry Acoder, but not working....

[php]
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;


if (frmObj.IRE.value.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]
Feb 15 '08 #9
acoder
16,027 Expert Mod 8TB
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.
Feb 15 '08 #10
viki1967
263 100+
What solution for this script ?
Feb 15 '08 #11
acoder
16,027 Expert Mod 8TB
What changes have you made to the script?
Feb 15 '08 #12
viki1967
263 100+
[php]
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;


if (frmObj.IRE.value.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]
Feb 15 '08 #13
acoder
16,027 Expert Mod 8TB
I've made some quick changes without testing to give you an idea:
Expand|Select|Wrap|Line Numbers
  1. function updateTime(txtObj)
  2. {
  3.   if (window.opener == null) return;
  4.  
  5.   var re = /^([01]\d|2[0-3]):[0-5]\d$/;
  6.  
  7.   if (txtObj.value.length >= 5 && !txtObj.value.match(re))
  8.     {
  9.       alert("Inserire l'orario nel formato 'hh:mm'.");
  10.       txtObj.focus();
  11.     }
  12.  
  13.  
  14.   var frmObj = txtObj.form;
  15.   var elems = frmObj.elements;
  16.  
  17.  
  18.   var result = "";
  19.   var destObj = window.opener.document.myform.impiego;
  20.  
  21.   var totalHours = 0;
  22.   var totalMins = 0;
  23.  
  24.       for ( var n = 0 ; n < elems.length ; n++ )
  25.       {
  26.  
  27.  
  28.           if (elems[n].type == "text" && elems[n].value.match(re))
  29.           {
  30.            var vals = elems[n].value.split(":");
  31.            var hours = parseInt(vals[0]);
  32.            var mins = parseInt(vals[1]);
  33.            totalHours += hours;
  34.            totalMins += mins;
  35.           }
  36.       } 
  37.   // validate outside the loop
  38.   if (totalHours > 7) {
  39.     alert("Error: total hours is more than 7");
  40.     return;
  41.   }
  42.   if ((totalHours == 7) && (totalMins > 36)) {
  43.     alert("Error: total time exceeds 7:36");
  44.     return;
  45.   }
  46. // validation complete...
  47.       for ( var n = 0 ; n < elems.length ; n++ )
  48.       {
  49.  
  50.           if (elems[n].type == "text" && elems[n].value.match(re))
  51.           {
  52.             {
  53.               if (result.length) result += ";"; 
  54.  
  55.               result += elems[n].name + "-" + elems[n].value;
  56.             }
  57.         }
  58.  
  59.       destObj.value= result;
  60.     }
  61. }
Feb 15 '08 #14
viki1967
263 100+
Thanks Acoder.
But 09:00 value is validated by function...
Feb 15 '08 #15
acoder
16,027 Expert Mod 8TB
Thanks Acoder.
But 09:00 value is validated by function...
Use the radix to parse the number as a decimal, i.e. use parseInt(val,10) as described here.
Feb 15 '08 #16
viki1967
263 100+
How to change this ?

var hours = parseInt(vals[0]);
var mins = parseInt(vals[1]);
Feb 15 '08 #17
acoder
16,027 Expert Mod 8TB
var hours = parseInt(vals[0],10);
var mins = parseInt(vals[1],10);
Feb 15 '08 #18
viki1967
263 100+
Thanks Acoder !!!!!!
Feb 15 '08 #19
acoder
16,027 Expert Mod 8TB
You're welcome.
Feb 15 '08 #20

Post your reply

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

Similar topics

1 post views Thread by Beringer | last post: by
1 post views Thread by greg7224 | last post: by
15 posts views Thread by Tinus | last post: by
1 post views Thread by Danny Ni | last post: by
1 post views Thread by Prakash | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.