I have a bit of a problem with my PHP form validation that I could use some help with.
I got an optional field which should be validated only if the user fills it, otherwise the form should be processed as normal. Here’s the validation for the field:
if (!preg_match("/(0?\d|1[0-2]):(0\d|[0-5]\d) (AM|PM)/i", '$start')) :
$errors->add('submit_error', __('<strong>ERROR</strong>: The time is incorrect', 'appthemes'));
endif;
Might be simple, but at the moment I just can’t get my head around how can I bypass the validation if the field is left empty. Any ideas?
PHP tests if the field is null before testing the regex.
If the field is null PHP will jump to the next instruction.