Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9099329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:35:12+00:00 2026-06-17T00:35:12+00:00

I have the following function: function calcTotal(event) {var myId = event.currentTarget.id; myId = myId.replace(/[^0-9]/g,

  • 0

I have the following function:

    function calcTotal(event)
    {var myId = event.currentTarget.id;

    myId = myId.replace(/[^0-9]/g, '');

    var timeRegex = /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;

    if($('#start'+myId).val().match(timeRegex) && $('#end'+myId).val().match(timeRegex) &&    $('#pause'+myId).val().match(timeRegex))
    {   alert('win');
        var minutes = 0;

        var n = $('#end'+myId).val().split(':');
        minutes = parseInt(n[0])*60 + parseInt(n[1]);

        var n = $('#start'+myId).val().split(':');
        minutes -= parseInt(n[0])*60 + parseInt(n[1]);

        var n = $('#pause'+myId).val().split(':');
        minutes -= parseInt(n[0])*60 + parseInt(n[1]);

        var hours = Math.floor(minutes/60);
        minutes = minutes % 60;
        alert(hours + ':' + minutes);
        $('#total' + myId).val(hours + ':' + minutes);
    }
    else
    {   alert('fail');
        $('#total' + myId).val('00:00');
    }

    }   
   </script>

Let’s assume myID equals 1 and the lets assume the following values:

start1 = 00:00
end1 = 10:00
pause1 = 02:00

I would like to for this function to match the previous three values to a regular expression to determine if they are valid times.
After that I would like to calculate the total time worked that day: end1-start1-pause1.
For some reason even with these correct values the function nevers enters the if(….){ alert(‘win’)…}.

I think there is something wrong with my regex, but I already checked (stackoverflow question) and used that regex.

Could anybody tell me what I am doing wrong?

the entire code:

<?php
if(isset($_POST['date']))
{       $sql = "INSERT INTO `hour_administration`.`hours` (`h_id`, `date`, `start`, `end`) VALUES";
    foreach($_POST['date'] as $k => $v)
    {
        if(empty($_POST['date'][$k]))
        break;
        if($k > 0)      
        $sql .= ",";        
        $sql .= "(NULL, '".$_POST['date'][$k]."',  '".$_POST['start_time'][$k].":00',  '".$_POST    ['end_time'][$k].":00')";

    }
    $sql .= ";";
    echo " Query: ".$sql. "</BR>";
    mysql_query($sql) or die('Could not query: ' . mysql_error());
}

if(isset($_POST['rows']))
{
    $rows = $_POST['rows'];
}else
{
$rows = 5;  
}




?>
<script>
$(document).ready(function() { 

    $( ".date" ).datepicker({ dateFormat: "yy-mm-dd" });
    $(".time").mask("99:99");           
    $('.start_time').change(function(e) {               
            calcTotal(e);

    });

    $('.end_time').change(function(e) {     
        calcTotal(e);

    });

    $('.pause').change(function(e) {        
        calcTotal(e);

    });
});

function calcTotal(event)
{   var myId = event.currentTarget.id;  
    myId = myId.replace(/[^0-9]/g, ''); 

    var timeRegex = /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;

    if($('#start'+myId).val().match(timeRegex) && $('#end'+myId).val().match(timeRegex) && $('#pause'+myId).val().match(timeRegex))
    {   
        var minutes = 0;

        var n = $('#end'+myId).val().split(':');
        minutes = parseInt(n[0])*60 + parseInt(n[1]);

        var n = $('#start'+myId).val().split(':');
        minutes -= parseInt(n[0])*60 + parseInt(n[1]);

        var n = $('#pause'+myId).val().split(':');
        minutes -= parseInt(n[0])*60 + parseInt(n[1]);

        var hours = Math.floor(minutes/60);
        minutes = minutes % 60;
        alert(hours + ':' + minutes);
        $('#total' + myId).val(hours + ':' + minutes);
    }
    else
    {   alert($('#start'+myId).val());
        $('#total' + myId).val('00:00');
    }

}   
   </script>

<div class="inner_container">
  <form name="input" method="POST" id="input">
    <table border="1">
      <tr>
        <td>Date</td>
        <td>Start time</td>
        <td>End time</td>
        <td>Pause</td>
        <td>Total</td>
      </tr>
      <tr>
        <?php for($i = 0;$i < $rows;$i++)
        {?>
        <td><input type="text" class="date" name="date[]" id="start<?php echo $i;?>"></td>
        <td><input type="text" class="start_time time"  name="start_time[]" id="start<?php echo $i;?>"></td>
        <td><input type="text" class="end_time time" name="end_time[]" id="end<?php echo $i;?>" ></td>
        <td><input type="text" class="pause time" name="pause[] " id="pause<?php echo $i;?>"></td>
        <td><input type="text" class="total" name="total[]" id="total<?php echo $i;?>"></td>
      </tr>
      <?php }?>
    </table>
    <input type="submit" id="submit_inner_container"value="Submit">
  </form>
  <form name="amount" method="POST">
    <label for="name">How many rows would you like?</label>
    <input type="text" name="rows" size="3" id="rows" value="<?php echo $rows; ?>"/>
    <input type="submit" value="Rows"
  </form>
</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:35:14+00:00Added an answer on June 17, 2026 at 12:35 am

    Your HTML shows that you have two input fields with the same id attribute:

        <td><input type="text" class="date" name="date[]" id="start<?php echo $i;?>"></td>
        <td><input type="text" class="start_time time"  name="start_time[]" id="start<?php echo $i;?>"></td>
    

    Should the first one here use “date” instead of “start”?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class in a php file: function calcTotal(){ var productID =
I have an element, this element calls the function calcTotal via the following code:
I have following function in jquery: $('canvas').createWindow('xyz', 500, 600); And js-code behind is: var
I have following function : function a() { var d = { foo :
I have the following function: public string MyPhrase(int val) { if ((val %3 ==
I have the following function first called with a button click. function chooseRosaryIN(evt:Event) {
I have the following function - def add (*nums) nums.reduce(:+) end def subtract(first, *rest)
I have the following function: testit() { [ $1 == OK ] && echo
I have the following function: function red(opt) { var sqr; sqr=r+parseInt(opt); hilight_css = {background-color:#f00};
I have the following function: $(function() { var address = $(#address).text(); var r; function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.