I have 2 input fields to input the duration of a CD song. The first input is for minutes and the second is for seconds
When submitting the form, I must insert in the db the duration either in seconds (minutes + seconds) or NULL. Here's my validation formula:
THIS CODE HAS BEEN EDITED WILL ALL THE GIVEN TIPS
// HTML
<input id="min" name="min" type="text" value="...">
<input id="sec" name="sec" type="text" value="...">
// prepare/sanitize variables for function
$min = ltrim(trim($_POST['min']), '0');
$sec = ltrim(trim($_POST['sec']), '0');
$warning = array();
$duration_in_secs = validate_duration($warning, $min, $sec, true);
function validate_duration(&$warning, $min, $sec, $required = false) {
$duration = NULL;
if (empty($min) && empty($sec)) {
if ($required) {
$warning['duration'] = "The duration is a required field";
}
} else {
if (!ctype_digit($min) || (int)$min < 0 || (int)$min > 59) {
$warning['min'] = 'The minutes must contain a positive numeric value between 0 and 59';
} else {
$duration += (int)$min * 60;
}
if (!ctype_digit($sec) || (int)$sec < 0 || (int)$sec > 59) {
$warning['sec'] = 'The seconds must contain a positive numeric value between 0 and 59';
} else {
$duration += (int)$sec;
}
}
return $duration;
}
$warningdoesn't seem to be accessible outside of the function? – Corbin Sep 30 '12 at 6:40150 seconds(maybe they are getting that info in total seconds from a different source/app and don't want to make the conversion)? – frozenkoi Oct 2 '12 at 8:23