Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #7307 > unrolled thread
| Started by | houghi <houghi@houghi.org.invalid> |
|---|---|
| First post | 2012-10-14 11:45 +0200 |
| Last post | 2012-10-14 21:28 +0200 |
| Articles | 20 on this page of 21 — 10 participants |
Back to article view | Back to comp.lang.php
Verifying time in php houghi <houghi@houghi.org.invalid> - 2012-10-14 11:45 +0200
Re: Verifying time in php Denis McMahon <denismfmcmahon@gmail.com> - 2012-10-14 10:25 +0000
Re: Verifying time in php houghi <houghi@houghi.org.invalid> - 2012-10-14 13:50 +0200
Re: Verifying time in php Denis McMahon <denismfmcmahon@gmail.com> - 2012-10-14 21:12 +0000
Re: Verifying time in php houghi <houghi@houghi.org.invalid> - 2012-10-14 12:30 +0200
Re: Verifying time in php "J.O. Aho" <user@example.net> - 2012-10-14 15:03 +0200
Re: Verifying time in php houghi <houghi@houghi.org.invalid> - 2012-10-14 17:16 +0200
Re: Verifying time in php Michael Fesser <netizen@gmx.de> - 2012-10-14 17:50 +0200
Re: Verifying time in php "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-10-14 18:22 +0200
Re: Verifying time in php "Peter H. Coffin" <hellsop@ninehells.com> - 2012-10-14 11:41 -0500
Re: Verifying time in php Martin Leese <please@see.Web.for.e-mail.INVALID> - 2012-10-14 11:06 -0600
Re: Verifying time in php houghi <houghi@houghi.org.invalid> - 2012-10-14 19:41 +0200
Re: Verifying time in php Michael Fesser <netizen@gmx.de> - 2012-10-14 21:03 +0200
Re: Verifying time in php houghi <houghi@houghi.org.invalid> - 2012-10-14 22:39 +0200
Re: Verifying time in php Christoph Becker <cmbecker69@gmx.de> - 2012-10-14 17:52 +0200
Re: Verifying time in php houghi <houghi@houghi.org.invalid> - 2012-10-14 19:20 +0200
Re: Verifying time in php "J.O. Aho" <user@example.net> - 2012-10-14 20:41 +0200
Re: Verifying time in php Christoph Becker <cmbecker69@gmx.de> - 2012-10-14 20:49 +0200
Re: Verifying time in php houghi <houghi@houghi.org.invalid> - 2012-10-14 22:32 +0200
Re: Verifying time in php Richard Damon <news.x.richarddamon@xoxy.net> - 2012-10-14 11:44 -0400
Re: Verifying time in php Luuk <luuk@invalid.lan> - 2012-10-14 21:28 +0200
Page 1 of 2 [1] 2 Next page →
| From | houghi <houghi@houghi.org.invalid> |
|---|---|
| Date | 2012-10-14 11:45 +0200 |
| Subject | Verifying time in php |
| Message-ID | <slrnk7l2e1.3f9.houghi@penne.houghi> |
I have a form that has the following entry fields: hours minutes seconds I need the time in seconds and that works: $realtime=$_POST['hours']*3600+$_POST['minutes']*60+$_POST['seconds']; However people can fill out nothing or they could enter characters What is the easiest way to verify if what the people filled out is correct? I have tried, but I get lost in the if statements. Extra difficult is that they must not fill out all three. Only one is enough. e.g. 1 hour and no minutes or second gives 3600 1 hour, 1 minute and 1 second gives 3661 1 hour and 'X' should give an error. All empty should give an error empty hour 70 minutes and 75 seconds gives 4275. Hours of more then 24 will be possible as well. I might even add number of days. If the last one gives an error, but it makes the life much easier, I can live with that It is the fact that they do not need to fill out all three that I am stuck. What I have tried is first to see if all are empty Next try to see if each one is not a number Then I need to figure out if there is a combination of some of it. And that is when I get lost in the if statements and loops in loops. I am sure there is an easy way to tell if all is nice and works well, but how? Seaching google gave me transfering from seconds to H:i:s, but that is not what I want. houghi -- This is written under the inluence of the following: > Artist : Jeff Wayne > Song : Horsell Common And The Heat Ray > Album : The War Of The Worlds (Disc 1)
[toc] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2012-10-14 10:25 +0000 |
| Message-ID | <k5e3ug$2cg$1@dont-email.me> |
| In reply to | #7307 |
On Sun, 14 Oct 2012 11:45:37 +0200, houghi wrote: > I have a form that has the following entry fields: > hours minutes seconds > > I need the time in seconds and that works: > $realtime=$_POST['hours']*3600+$_POST['minutes']*60+$_POST['seconds']; > > However people can fill out nothing or they could enter characters > > What is the easiest way to verify if what the people filled out is > correct? I have tried, but I get lost in the if statements. Verify it is correct in what way? If you want to record the time the data was entered, it would make more sense to use the system time, and not collect the data in the form at all. If you want the user to specify the time that an event happened in the past, or is to happen in the future, give them a select list with options of 1-31 for day, Jan-Dec for month, <whatever range suits> for year, 00-23 for hours, 00-59 for mins, 00-59 for seconds. Convert the form data to an actual timestamp and do whatever validation and verification you want. Rgds Denis McMahon
[toc] | [prev] | [next] | [standalone]
| From | houghi <houghi@houghi.org.invalid> |
|---|---|
| Date | 2012-10-14 13:50 +0200 |
| Message-ID | <slrnk7l9or.3f9.houghi@penne.houghi> |
| In reply to | #7308 |
Denis McMahon wrote: >> What is the easiest way to verify if what the people filled out is >> correct? I have tried, but I get lost in the if statements. > > Verify it is correct in what way? > > If you want to record the time the data was entered, it would make more > sense to use the system time, and not collect the data in the form at all. > > If you want the user to specify the time that an event happened in the > past, or is to happen in the future, give them a select list with options > of 1-31 for day, Jan-Dec for month, <whatever range suits> for year, > 00-23 for hours, 00-59 for mins, 00-59 for seconds. Convert the form data > to an actual timestamp and do whatever validation and verification you > want. Sorry for the confiusion. I hope the link I gave clariefied things a bit. http://houghi/gopro.php You can enter the number of hours, minutes and seconds yourself. So it is to verify if the human entry is correct. This is used for calcualtions. Say that somebody would like to do a timelaps over a period of 8 hours with an interval of 1 image every second and a framrate of 30. This would result in a movie that is 16 minutes long. The time can be almost anything. You could have a project that takes 10 days (240 hours) and with a 60 second interval that would be an 8 minute movie. So one can fill out hours, minutes and seconds. However if nothing is filled out, it is not correct. At least one must be filled out. Also it must be numbers and not nececerily limited to 59 minutes. If you have a detail of 117 minutes, you should be able to do that and not first calculate back to 1 hour and 57 minutes. So dropdown is not an option (And I dislike dropdown options for minutes) So it is not so much time as it is duration. I am able to test each if there is a numeric field. I can't get the combination of the fields working: 1) One of the time fields must be filled out. 2) None of the time fields must contain non-digits. houghi -- Dr. Walter Gibbs: Won't that be grand? Computers and the programs will start thinking and the people will stop. -- Tron (1982)
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2012-10-14 21:12 +0000 |
| Message-ID | <k5f9rk$gen$1@dont-email.me> |
| In reply to | #7310 |
On Sun, 14 Oct 2012 13:50:51 +0200, houghi wrote: > Denis McMahon wrote: >>> What is the easiest way to verify if what the people filled out is >>> correct? I have tried, but I get lost in the if statements. >> >> Verify it is correct in what way? >> >> If you want to record the time the data was entered, it would make more >> sense to use the system time, and not collect the data in the form at >> all. >> >> If you want the user to specify the time that an event happened in the >> past, or is to happen in the future, give them a select list with >> options of 1-31 for day, Jan-Dec for month, <whatever range suits> for >> year, 00-23 for hours, 00-59 for mins, 00-59 for seconds. Convert the >> form data to an actual timestamp and do whatever validation and >> verification you want. > > Sorry for the confiusion. I hope the link I gave clariefied things a > bit. http://houghi/gopro.php You can enter the number of hours, minutes > and seconds yourself. > So it is to verify if the human entry is correct. > This is used for calcualtions. > > Say that somebody would like to do a timelaps over a period of 8 hours > with an interval of 1 image every second and a framrate of 30. This > would result in a movie that is 16 minutes long. > > The time can be almost anything. You could have a project that takes 10 > days (240 hours) and with a 60 second interval that would be an 8 minute > movie. > > So one can fill out hours, minutes and seconds. However if nothing is > filled out, it is not correct. At least one must be filled out. > Also it must be numbers and not nececerily limited to 59 minutes. If you > have a detail of 117 minutes, you should be able to do that and not > first calculate back to 1 hour and 57 minutes. > So dropdown is not an option (And I dislike dropdown options for > minutes) > > So it is not so much time as it is duration. > > I am able to test each if there is a numeric field. I can't get the > combination of the fields working: > 1) One of the time fields must be filled out. > 2) None of the time fields must contain non-digits. OK, given three values in your form: <p>Hours: <input type="text" size="4" name="hours"><br> Minutes: <input type="text" size="4" name="mins"><br> Seconds: <input type="text" size="4" name="secs"></p> Then: <?php $secs = 0; if ( isset( $_POST["hours"] ) ) $secs += 3600 * intval( $_POST["hours"] ); if ( isset( $_POST["mins"] ) ) $secs += 60 * intval( $_POST["mins"] ); if ( isset( $_POST["secs"] ) ) $secs += intval( $_POST["secs"] ); // $secs is now the combined value of // whatever your hours, minutes and // seconds inputs were, apply some limits $secs = max( $secs, 1 ); $secs = min( $secs, 86400 ); // now if you want you can normalise $secs // into hours : minutes : seconds $hh = $secs / 3600; $mm = ( $secs - $hh * 3600 ) / 60; $ss = ( $secs - $hh * 3600 ) % 60; $timestr = sprintf( "%02d:%02d:%02d", $hh, $mm, $ss ); ?> Unless I've totally misunderstood the problem, which is quite possible. Rgds Denis McMahon
[toc] | [prev] | [next] | [standalone]
| From | houghi <houghi@houghi.org.invalid> |
|---|---|
| Date | 2012-10-14 12:30 +0200 |
| Message-ID | <slrnk7l52o.3f9.houghi@penne.houghi> |
| In reply to | #7307 |
houghi wrote: > I have a form that has the following entry fields: > hours > minutes > seconds The page can be found here: http://houghi.org/gopro.php The code can be seen here : http://houghi.org/gopro.php?code What I want to know if the $realtime is something I could get an error message form. Please do not comment on the rest of the code inbthis tread. I know it is ugly and wrong i other places. I just want to figure out how I can check if Hours, Minutes and Seconds input is valid. houghi -- This is written under the inluence of the following: > Artist : James Brown > Song : Get Up Offa That Thing (Release The Pressure) > Album : Star Time
[toc] | [prev] | [next] | [standalone]
| From | "J.O. Aho" <user@example.net> |
|---|---|
| Date | 2012-10-14 15:03 +0200 |
| Message-ID | <advrhmFroasU1@mid.individual.net> |
| In reply to | #7309 |
On 14/10/12 12:30, houghi wrote: > houghi wrote: >> I have a form that has the following entry fields: >> hours >> minutes >> seconds > > The code can be seen here : http://houghi.org/gopro.php?code > > $realtime=$_POST['hours']*3600+$_POST['minutes']*60+$_POST['seconds']; > > What I want to know if the $realtime is something I could get an error > message form. You would in most cases have a value of some sort, but you can't always relay on that it will for ever be like that (we know PHP makes some big changes from time to time) and what happens further down if you have negative values or huge values. Validate what you get and make a sanity check on what you got before you calculate the realtime value. > I just want to figure out how I can check if Hours, Minutes and Seconds > input is valid. There are quite many is_* functions, among those you have is_numeric()[1] and another one is isset()[2] which gives you true or false. [1]: http://www.php.net/manual/en/function.is-numeric.php [2]: http://www.php.net/manual/en/function.isset.php -- //Aho
[toc] | [prev] | [next] | [standalone]
| From | houghi <houghi@houghi.org.invalid> |
|---|---|
| Date | 2012-10-14 17:16 +0200 |
| Message-ID | <slrnk7llq2.3f9.houghi@penne.houghi> |
| In reply to | #7311 |
J.O. Aho wrote: > Validate what you get and make a sanity check on what you got before you > calculate the realtime value. > > >> I just want to figure out how I can check if Hours, Minutes and Seconds >> input is valid. > > There are quite many is_* functions, among those you have > is_numeric()[1] and another one is isset()[2] which gives you true or false. > > > [1]: http://www.php.net/manual/en/function.is-numeric.php > [2]: http://www.php.net/manual/en/function.isset.php I am aware of those. However I can not figure out how to do a check on all of them, without getting lost in if statements. See below where '-' means empty, 1 means any digit(s) and A means any no digit(s). I have not shown all options. Valid? $hours $min $sec No - - - Yes 1 - - Yes 1 - 1 Yes - 1 - Yes 1 1 1 No A - - No 1 A - No 1A - - So I have to figure out if some are filled out and if they are, if they are digits. Looking around for 2 days now to make it intro something can read next week as well. I keep getting back to where I need to get through all options. So is_numeric is obvious as well as is_set, but is_set is not needed if others are set. houghi -- This is written under the inluence of the following: > Artist : Apocalyptica > Song : Hope (Vol.2) (Radio Edit) > Album : Hope Vol.2
[toc] | [prev] | [next] | [standalone]
| From | Michael Fesser <netizen@gmx.de> |
|---|---|
| Date | 2012-10-14 17:50 +0200 |
| Message-ID | <rhnl78lfjj7jv81qqbofvp0334fbkdrv0e@mfesser.de> |
| In reply to | #7312 |
.oO(houghi) >I am aware of those. However I can not figure out how to do a check on >all of them, without getting lost in if statements. > >See below where '-' means empty, 1 means any digit(s) and A means any no >digit(s). I have not shown all options. >[...] I wouldn't care too much about invalid inputs. Instead I would just take what I get and see what I can do with it. So I would explicitly cast all values to int (invalid or empty values will become 0), then just do a simple range check on all of the three values and then do the math. Micha -- http://mfesser.de/ Fotos | Blog | Flohmarkt
[toc] | [prev] | [next] | [standalone]
| From | "M. Strobel" <sorry_no_mail_here@nowhere.dee> |
|---|---|
| Date | 2012-10-14 18:22 +0200 |
| Message-ID | <ae0770Fufi2U1@mid.uni-berlin.de> |
| In reply to | #7314 |
Am 14.10.2012 17:50, schrieb Michael Fesser: > .oO(houghi) > >> I am aware of those. However I can not figure out how to do a check on >> all of them, without getting lost in if statements. >> >> See below where '-' means empty, 1 means any digit(s) and A means any no >> digit(s). I have not shown all options. >> [...] > > I wouldn't care too much about invalid inputs. Instead I would just take > what I get and see what I can do with it. > > So I would explicitly cast all values to int (invalid or empty values > will become 0), then just do a simple range check on all of the three > values and then do the math. +1 /Str.
[toc] | [prev] | [next] | [standalone]
| From | "Peter H. Coffin" <hellsop@ninehells.com> |
|---|---|
| Date | 2012-10-14 11:41 -0500 |
| Message-ID | <slrnk7lqq2.4ja.hellsop@nibelheim.ninehells.com> |
| In reply to | #7314 |
On Sun, 14 Oct 2012 17:50:48 +0200, Michael Fesser wrote: > .oO(houghi) > >>I am aware of those. However I can not figure out how to do a check on >>all of them, without getting lost in if statements. >> >>See below where '-' means empty, 1 means any digit(s) and A means any no >>digit(s). I have not shown all options. >>[...] > > I wouldn't care too much about invalid inputs. Instead I would just take > what I get and see what I can do with it. > > So I would explicitly cast all values to int (invalid or empty values > will become 0), then just do a simple range check on all of the three > values and then do the math. Challenging curcumstance: "A" "B" "C" cast to int becomes 0:0:0, which is a valid time. Use the values submitted to make a valid format to throw at strptime() perhaps... If it returns FALSE, it's not a valid time. Anything else is. -- It is impossible to sharpen a pencil with a blunt axe. It is equally vain to try to do it with ten blunt axes instead --E.W Dijkstra, 1930-2002
[toc] | [prev] | [next] | [standalone]
| From | Martin Leese <please@see.Web.for.e-mail.INVALID> |
|---|---|
| Date | 2012-10-14 11:06 -0600 |
| Message-ID | <k5ereu$i87$1@dont-email.me> |
| In reply to | #7317 |
Peter H. Coffin wrote: > On Sun, 14 Oct 2012 17:50:48 +0200, Michael Fesser wrote: >> I wouldn't care too much about invalid inputs. Instead I would just take >> what I get and see what I can do with it. >> >> So I would explicitly cast all values to int (invalid or empty values >> will become 0), then just do a simple range check on all of the three >> values and then do the math. > > Challenging curcumstance: "A" "B" "C" cast to int becomes 0:0:0, which > is a valid time. But not a valid *duration* (which is what the OP actually wants). -- Regards, Martin Leese E-mail: please@see.Web.for.e-mail.INVALID Web: http://members.tripod.com/martin_leese/
[toc] | [prev] | [next] | [standalone]
| From | houghi <houghi@houghi.org.invalid> |
|---|---|
| Date | 2012-10-14 19:41 +0200 |
| Message-ID | <slrnk7lu9j.3f9.houghi@penne.houghi> |
| In reply to | #7318 |
Martin Leese wrote: >> >> Challenging curcumstance: "A" "B" "C" cast to int becomes 0:0:0, which >> is a valid time. > > But not a valid *duration* (which is what > the OP actually wants). Indeed and that is where I got stuck several times. So I started thinking outside the box and for the test I do not care what the values are, as long as they are values. I then came up with if 1, 2 and 3 are ok, then 123 is OK as well. If 4, Y and <empty> is not OK, then 4Y is not ok either. As far as internationalisation goes, a dot will fork, a comma won't. Even though I live in a country where should use a comma, I always se a dot. houghi -- This is written under the inluence of the following: > Artist : Santana > Song : Europa (Earth's Heaven's Smile) > Album : Moonflower
[toc] | [prev] | [next] | [standalone]
| From | Michael Fesser <netizen@gmx.de> |
|---|---|
| Date | 2012-10-14 21:03 +0200 |
| Message-ID | <qe2m78t97m2bcm40j49hp8umjhj9el385m@mfesser.de> |
| In reply to | #7317 |
.oO(Peter H. Coffin) >On Sun, 14 Oct 2012 17:50:48 +0200, Michael Fesser wrote: >> .oO(houghi) >> >>>I am aware of those. However I can not figure out how to do a check on >>>all of them, without getting lost in if statements. >>> >>>See below where '-' means empty, 1 means any digit(s) and A means any no >>>digit(s). I have not shown all options. >>>[...] >> >> I wouldn't care too much about invalid inputs. Instead I would just take >> what I get and see what I can do with it. >> >> So I would explicitly cast all values to int (invalid or empty values >> will become 0), then just do a simple range check on all of the three >> values and then do the math. > >Challenging curcumstance: "A" "B" "C" cast to int becomes 0:0:0, which >is a valid time. Yes, as expected and intended. In my scripts I do a similar thing with date inputs. I don't like separate fields for day, month and year. So I simply accept what I get from a single field and try to parse it in several ways. If the result is a valid date, I accept it. If not, it triggers an error message and shows the form again. And I would do the same here as described above. If all three values are empty or invalid, the result will automatically be 0 and can easily be rejected. And if values like 36h or 90m are accepted as well, even the range check could be omitted: just cast to int, maybe abs(), and then calculate the time in seconds, that's it. Why make things more difficult than necessary? Micha -- http://mfesser.de/ Fotos | Blog | Flohmarkt
[toc] | [prev] | [next] | [standalone]
| From | houghi <houghi@houghi.org.invalid> |
|---|---|
| Date | 2012-10-14 22:39 +0200 |
| Message-ID | <slrnk7m8nc.3f9.houghi@penne.houghi> |
| In reply to | #7323 |
Michael Fesser wrote: > And I would do the same here as described above. If all three values are > empty or invalid, the result will automatically be 0 and can easily be > rejected. And if values like 36h or 90m are accepted as well, even the > range check could be omitted: just cast to int, maybe abs(), and then > calculate the time in seconds, that's it. Why make things more difficult > than necessary? I make things more difficult, because some people will always count in minutes when making movies and others will not. So 80 will be hours for one and minutes for another. And then there will yet be others wo do 16:34. So will that be 16 hours and 34 minutes or 16 minutes and 34 seconds? houghi -- You are about to enter another dimension, a dimension not only of sight and sound but of mind. A journey into a wondrous land of imagination. Next stop, Usenet!
[toc] | [prev] | [next] | [standalone]
| From | Christoph Becker <cmbecker69@gmx.de> |
|---|---|
| Date | 2012-10-14 17:52 +0200 |
| Message-ID | <k5en2q$7cs$1@speranza.aioe.org> |
| In reply to | #7312 |
houghi wrote:
> However I can not figure out how to do a check on all of them,
> without getting lost in if statements.
How about:
$ok = 0;
if (is_valid($hours)) {$ok++;}
if (is_valid($min)) {$ok++;}
if (is_valid($sec)) {$ok++;}
if ($ok) {
// fine :-)
} else {
// report error
}
--
Christoph M. Becker
[toc] | [prev] | [next] | [standalone]
| From | houghi <houghi@houghi.org.invalid> |
|---|---|
| Date | 2012-10-14 19:20 +0200 |
| Message-ID | <slrnk7lt3n.3f9.houghi@penne.houghi> |
| In reply to | #7312 |
houghi wrote: > Valid?$hours $min $sec > No - - - > Yes 1 - - > Yes 1 - 1 > Yes - 1 - > Yes 1 1 1 > No A - - > No 1 A - > No 1A - - > > So I have to figure out if some are filled out and if they are, if they > are digits. Looking around for 2 days now to make it intro something > can read next week as well. > > I keep getting back to where I need to get through all options. > > So is_numeric is obvious as well as is_set, but is_set is not needed if > others are set. Found finaly the solution. Instead of looking at the seperate values and then trying to combine them, it is better to combine them and then evaluate them with $testtime=$_POST['hours'].$_POST['minutes'].$_POST['seconds']; $hours $min $sec $testtime Valid? - - - - No 1 - - 1 Yes 1 - 1 11 Yes - 1 - 1 Yes 1 1 1 111 Yes A - - A No 1 A - 1A No 1A - - 1A No That way I can just do a 'is_numeric($testtime)' and no need to use is_set or anything else. houghi -- This is written under the inluence of the following: > Artist : Nightwish > Song : Reach (Amaranth Demo Version) > Album : Amaranth
[toc] | [prev] | [next] | [standalone]
| From | "J.O. Aho" <user@example.net> |
|---|---|
| Date | 2012-10-14 20:41 +0200 |
| Message-ID | <ae0fb0F206uU1@mid.individual.net> |
| In reply to | #7319 |
On 14/10/12 19:20, houghi wrote:
> Found finaly the solution. Instead of looking at the seperate values and
> then trying to combine them, it is better to combine them and then
> evaluate them with
> $testtime=$_POST['hours'].$_POST['minutes'].$_POST['seconds'];
>
> $hours $min $sec $testtime Valid?
> - - - - No
> 1 - - 1 Yes
> 1 - 1 11 Yes
> - 1 - 1 Yes
> 1 1 1 111 Yes
> A - - A No
> 1 A - 1A No
> 1A - - 1A No
>
> That way I can just do a 'is_numeric($testtime)' and no need to use
> is_set or anything else.
an alternative is:
if you have <input name="variable[xxx]" ...> in your form
then you can in your script
$ok = true;
foreach($_POST['variable'] as $value) {
if(!is_numeric($value))
$ok = false;
}
even if your method works now, I wouldn't bet on it in the future.
--
//Aho
[toc] | [prev] | [next] | [standalone]
| From | Christoph Becker <cmbecker69@gmx.de> |
|---|---|
| Date | 2012-10-14 20:49 +0200 |
| Message-ID | <k5f1d5$1dn$1@speranza.aioe.org> |
| In reply to | #7319 |
houghi wrote: > Instead of looking at the seperate values and > then trying to combine them, it is better to combine them and then > evaluate them with > $testtime=$_POST['hours'].$_POST['minutes'].$_POST['seconds']; Did you consider the following input values? $hours $min $sec $testtime Valid? 0 x 12 0x12 Yes -1 10 20 -11020 Yes 1 . 1 1.1 Yes 1 2e 34 12e34 Yes -- Christoph M. Becker
[toc] | [prev] | [next] | [standalone]
| From | houghi <houghi@houghi.org.invalid> |
|---|---|
| Date | 2012-10-14 22:32 +0200 |
| Message-ID | <slrnk7m8an.3f9.houghi@penne.houghi> |
| In reply to | #7322 |
Christoph Becker wrote: > Did you consider the following input values? > > $hours $min $sec $testtime Valid? > 0 x 12 0x12 Yes > -1 10 20 -11020 Yes > 1 . 1 1.1 Yes > 1 2e 34 12e34 Yes No. Thanks for the pointer. houghi -- You are about to enter another dimension, a dimension not only of sight and sound but of mind. A journey into a wondrous land of imagination. Next stop, Usenet!
[toc] | [prev] | [next] | [standalone]
| From | Richard Damon <news.x.richarddamon@xoxy.net> |
|---|---|
| Date | 2012-10-14 11:44 -0400 |
| Message-ID | <k5emks$ls2$1@dont-email.me> |
| In reply to | #7307 |
On 10/14/12 5:45 AM, houghi wrote: > I have a form that has the following entry fields: > hours > minutes > seconds > > I need the time in seconds and that works: > $realtime=$_POST['hours']*3600+$_POST['minutes']*60+$_POST['seconds']; > > However people can fill out nothing or they could enter characters > > What is the easiest way to verify if what the people filled out is > correct? I have tried, but I get lost in the if statements. > > Extra difficult is that they must not fill out all three. Only one is > enough. e.g. > 1 hour and no minutes or second gives 3600 > 1 hour, 1 minute and 1 second gives 3661 > 1 hour and 'X' should give an error. > All empty should give an error > empty hour 70 minutes and 75 seconds gives 4275. > Hours of more then 24 will be possible as well. > I might even add number of days. > > If the last one gives an error, but it makes the life much easier, I can > live with that > > It is the fact that they do not need to fill out all three that I am > stuck. > > What I have tried is first to see if all are empty > Next try to see if each one is not a number > Then I need to figure out if there is a combination of some of it. > > And that is when I get lost in the if statements and loops in loops. > > I am sure there is an easy way to tell if all is nice and works well, > but how? Seaching google gave me transfering from seconds to H:i:s, but > that is not what I want. > > houghi > First you need a definition of "Valid", it sounds like for your case, the definition would be that each field contains only the characters 0-9 or is blank (which in some sense is a special case of only containing the characters 0-9), with a second restriction of all can not be blank. From your later discussion, it sounds like a result of all fields 0 or blank is also invalid, for which the simplest test is probably do you calculation and then see if the result is 0. You might also want the fields to contain a possible single decimal point so the person could enter something like 3.5 hours as a time. The biggest question with this option is do you need to support internationalization, where sometimes a decimal point is a '.' and sometimes it is a ','. The simplest structure for this sounds to be code that first checks each field that is contains the proper characters, the compute the results and see if the value is 0. To fully test a field, if we are willing to ignore internationalization (that is left as an exercise to the reader) is to match the fields to the regular expression (warning, untested): ^\d*(\.\d*)? You may also want to modify the test string to allow whitespace before and after the string of digits.
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.php
csiph-web