/    Sign up×
Community /Pin to ProfileBookmark

Help with validation please

Hello there! I hope you can help me please.

Currently, we obtain rotas for our staff via a normal form, which is then edited by a PHP mailer and then shipped to our staffing coordinator. We are now making this a little more interactive, in the form that the individual themselves can “update” their own rota, and see their current one.

What I need help with is validating the submission to check we have correct information. When I’m trying, it’s turning out absolutely massive!

What I want to achieve is…:

  • To check the hours are given as HH:MM (with the : dividier)

  • At least one start and finish, OR a not online is given, so a day has some data.

  • Finish isn’t earlier than the start, or the addiitonal hours conflict.

  • Hour is either 00 or 01->23, and mins don’t exceed 60
  • The code:

    [code]
    <table class=”forms” cellpadding=”5″ cellspacing=”0″ border=”0″>
    <tr>
    <td class=”bg small”>&nbsp;</td>
    <td class=”bg small”><strong>Start:</strong></td>
    <td class=”bg small”><strong>Finish:</strong></td>
    <td colspan=”2″ class=”bg small”><strong>Additional:</strong></td>
    <td class=”bg small”><strong>Not online:</strong></td>
    </tr>

    <tr>
    <td class=”details small”><strong>E.g:</strong></td>
    <td class=”details small”><input type=”text” disabled=”disabled” name=”EXAMPLE2-START” size=”6″ value=”13:00″ /></td>
    <td class=”details small”>-> <input type=”text” disabled=”disabled” name=”EXAMPLE1-FINISH” size=”6″ value=”19:00″ /></td>
    <td class=”details small”><input type=”text” disabled=”disabled” name=”EXAMPLE2-START” size=”6″ value=”22:00″ /></td>
    <td class=”details small”>-> <input type=”text” disabled=”disabled” name=”EXAMPLE2-FINISH” size=”6″ value=”23:30″ /></td>
    <td class=”details small”><input name=”EXAMPLE-NOTONLINE” disabled=”disabled” type=”checkbox” class=”noborder” value=”Not online” /></td>
    </tr>

    <tr>
    <td class=”details small”><strong>Sun:</strong></td>
    <td class=”details small”><input type=”text” name=”start1[0]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”>-> <input type=”text” name=”finish1[0]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”><input type=”text” name=”start2[0]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”>-> <input type=”text” name=”finish2[0]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”><input name=”notonline[0]” type=”checkbox” class=”noborder” value=”Not online” /></td>
    </tr>

    <tr>
    <td class=”details small”><strong>Mon:</strong></td>
    <td class=”details small”><input type=”text” name=”start1[1]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”>-> <input type=”text” name=”finish1[1]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”><input type=”text” name=”start2[1]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”>-> <input type=”text” name=”finish2[1]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”><input name=”notonline[1]” type=”checkbox” class=”noborder” value=”Not online” /></td>
    </tr>

    <tr>
    <td class=”details small”><strong>Tue:</strong></td>
    <td class=”details small”><input type=”text” name=”start1[2]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”>-> <input type=”text” name=”finish1[2]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”><input type=”text” name=”start2[2]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”>-> <input type=”text” name=”finish2[2]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”><input name=”notonline[2]” type=”checkbox” class=”noborder” value=”Not online” /></td>
    </tr>

    <tr>
    <td class=”details small”><strong>Wed:</strong></td>
    <td class=”details small”><input type=”text” name=”start1[3]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”>-> <input type=”text” name=”finish1[3]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”><input type=”text” name=”start2[3]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”>-> <input type=”text” name=”finish2[3]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”><input name=”notonline[3]” type=”checkbox” class=”noborder” value=”Not online” /></td>
    </tr>

    <tr>
    <td class=”details small”><strong>Thu:</strong></td>
    <td class=”details small”><input type=”text” name=”start1[]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”>-> <input type=”text” name=”finish1[4]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”><input type=”text” name=”start2[4]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”>-> <input type=”text” name=”finish2[4]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”><input name=”notonline[4]” type=”checkbox” class=”noborder” value=”Not online” /></td>
    </tr>

    <tr>
    <td class=”details small”><strong>Fri:</strong></td>
    <td class=”details small”><input type=”text” name=”start1[5]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”>-> <input type=”text” name=”finish1[5]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”><input type=”text” name=”start2[5]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”>-> <input type=”text” name=”finish2[5]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”><input name=”notonline[5]” type=”checkbox” class=”noborder” value=”Not online” /></td>
    </tr>

    <tr>
    <td class=”details small”><strong>Sat:</strong></td>
    <td class=”details small”><input type=”text” name=”start1[6]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”>-> <input type=”text” name=”finish1[6]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”><input type=”text” name=”start2[6]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”>-> <input type=”text” name=”finish2[6]” size=”6″ maxlength=”5″ /></td>
    <td class=”details small”><input name=”notonline[6]” type=”checkbox” class=”noborder” value=”Not online” /></td>
    </tr>
    </table>
    [/code]

    Any help would be very much appreciated.
    Thank you!

    to post a comment
    PHP

    11 Comments(s)

    Copy linkTweet thisAlerts:
    @DanUKauthorMar 19.2007 — Any ideas on this guys, please? ?

    Thanks in advance
    Copy linkTweet thisAlerts:
    @bokehMar 19.2007 — Maybe noone has replied because your question is not explained well.
    Copy linkTweet thisAlerts:
    @DanUKauthorMar 19.2007 — I don't know how to explain it and clearer in the bullets of what I'm trying to achieve. Could you let me know specifically what's not understood?

    Thanks
    Copy linkTweet thisAlerts:
    @bokehMar 19.2007 — Well first you talk about rotas but don't explain what they are. Then you talk about validation but you don't say what needs validating. Then you talk about what you are trying to achieve but don't explain how that relates to your post. Then you post a block of HTML mark-up (a table of all things) that is not related to PHP in any way.

    No forms or anything that would help decipher what you are asking has been posted.
    Copy linkTweet thisAlerts:
    @DanUKauthorMar 19.2007 — Hi there - okay... I'll try and explain it simpler.

    The HTML I pasted is the html table that contains the rota that needs valdating. There is no PHP yet, because that's what my question is about.

    By "normal form" I refer to the pasted HTML - that's in a simple <form> which posts to a .php page, where we need the validation to be.

    On this example:

    <i>
    </i> &lt;tr&gt;
    &lt;td class="details small"&gt;&lt;strong&gt;Sun:&lt;/strong&gt;&lt;/td&gt;
    &lt;td class="details small"&gt;&lt;input type="text" name="start1[0]" size="6" maxlength="5" /&gt;&lt;/td&gt;
    &lt;td class="details small"&gt;-&gt; &lt;input type="text" name="finish1[0]" size="6" maxlength="5" /&gt;&lt;/td&gt;
    &lt;td class="details small"&gt;&lt;input type="text" name="start2[0]" size="6" maxlength="5" /&gt;&lt;/td&gt;
    &lt;td class="details small"&gt;-&gt; &lt;input type="text" name="finish2[0]" size="6" maxlength="5" /&gt;&lt;/td&gt;
    &lt;td class="details small"&gt;&lt;input name="notonline[0]" type="checkbox" class="noborder" value="Not online" /&gt;&lt;/td&gt;
    &lt;/tr&gt;


    start1[0] is the fist start time of the day, followed by finish1[0] which is the finish time of it. There is then a further start2[0] for another given time, or Not online if the person is not online that day.

    The number in the brackets ie [1] or [0] is the day

    The rotas then continue in this format, for each day.

    I hope this is a bit easier - I really can't explain it any simpler
    Copy linkTweet thisAlerts:
    @bokehMar 19.2007 — [code=php]<?php

    $start = '08:00';
    $end = '16:00';

    // - To check the hours are given as HH:MM (with the : dividier)
    // - Hour is either 00 or 01->23, and mins don't exceed 60
    preg_match('/^([01]|2(?=[0-3]))d:[0-5]d$/', $start) or die('Start time is invalid');
    preg_match('/^([01]|2(?=[0-3]))d:[0-5]d$/', $end ) or die('End time is invalid');

    // - Finish isn't earlier than the start
    if(substr($start,0,2).substr($start,-2) > substr($end,0,2).substr($end,-2))
    {
    die('Invalid time inputs');
    }

    echo 'Passed these tests';

    ?>[/code]
    Copy linkTweet thisAlerts:
    @DanUKauthorAug 26.2007 — Hi there bokeh

    Thanks for the code, and apologies for not responding sooner to this thread, I kind of forgot it was here!!!

    The processing code for the Rotas is the following PHP, as you will see it goes through as an array to work out the total number of hours per day, and a total per week.

    [code=php]
    function totalHours ($d, &$weektot) {
    $s1 = $_POST['start1'][$d] ? strtotime('2006-01-01 '.$_POST['start1'][$d].':00') : 0;
    $s2 = $_POST['start2'][$d] ? strtotime('2006-01-01 '.$_POST['start2'][$d].':00') : 0;
    $f1 = $_POST['finish1'][$d] ? strtotime('2006-01-01 '.$_POST['finish1'][$d].':00') : 0;
    $f2 = $_POST['finish2'][$d] ? strtotime('2006-01-01 '.$_POST['finish2'][$d].':00') : 0;
    $totmins = floor((($f1 + $f2) - ($s1 + $s2))/60);
    if ($totmins < 0) $totmins += 24*60;
    $weektot += $totmins;
    return sprintf('%2d:%02d', floor($totmins/60), $totmins%60);
    }

    $days = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
    $weektot = 0;
    if (isset($_POST['start1'])) {
    for ($d=0; $d < 7; $d++) {
    if (isset($_POST['notonline'][$d])) {
    $hours .= sprintf('%-6s %s%s',
    str_pad($days[$d],6,'.'),'Not online',"n");
    }
    else {
    $hours .= sprintf('%-6s %5s -> %5s, %5s %s %5s (Total: %5s hrs)%s',
    str_pad($days[$d],6,'.'),
    $_POST['start1'][$d],
    $_POST['finish1'][$d],
    $_POST['start2'][$d],
    $_POST['start2'][$d] ? '->' : ' ',
    $_POST['finish2'][$d],
    totalHours($d, $weektot),
    "n"
    );
    }
    }
    $hours .= sprintf('%37s __________________%s', '', "n");
    $hours .= sprintf('%37s (Total:%3d:%02d hrs)%s',
    str_pad('Weekly hours',37,'.'),
    floor($weektot/60),
    $weektot%60,
    "n");
    }
    [/code]


    This works perfectly, however, based on that - how can we make your validation event apply to all the passed hours?

    As you will notice, many things can go wrong - I'm glad your validation event checks for the ":", checks hours and minutes, but also in addition we need to check if a start time is given, and end time is to.

    Any help applying your function to the above would be much appreciated.

    Once again, thanks!
    Copy linkTweet thisAlerts:
    @bokehAug 27.2007 — we need to check if a start time is given, and end time is to.[/QUOTE]That's what this does (note the "[I]or die[/I]"):[code=php]preg_match('/^([01]|2(?=[0-3]))d:[0-5]d$/', $start) or die('Start time is invalid');
    preg_match('/^([01]|2(?=[0-3]))d:[0-5]d$/', $end ) or die('End time is invalid'); [/code]
    Copy linkTweet thisAlerts:
    @DanUKauthorAug 27.2007 — Ahh, okay, great!

    Based on how it is now, how can I incorporate that to check all the inputs? At the moment, the HTML for each of the start/end times is on the top post of this thread, the processing part is the PHP I just included. How can I apply your validation code to check them all?

    Thanks, I really appreciate your help
    Copy linkTweet thisAlerts:
    @DanUKauthorSep 02.2007 — Hi again

    Would I be right in assuming I should be passing all the times in an array to $start and $end? That's what I'm trying, but I'm still not having much luck ?

    Thanks again
    Copy linkTweet thisAlerts:
    @DanUKauthorSep 06.2007 — I'm really sorry, but I still cannot get this working for it to check all the post data of hours (from the original HTML on my first post, along with the PHP in further posts)

    I have tried using arrays in $start and $end, but I'm not getting anywhere

    Any help would be much appreciated with this ?
    ×

    Success!

    Help @DanUK spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...