Jump to content

Validating time


ShaolinF

Recommended Posts

The approach seems ok. You would probably want to use the ^ and $ characters otherwise you could get problem with somebody entering "something 23:03 something else" since the code in the link would validate that as ok. Personally I don't see the point of using the {1}'s since without a repeat character it will only match one character anyway. You could possibly also convert the group to none capture which may make it slightly more efficient (I'm fairly novice so I couldn't swear to that one but since you don't need the value no point in capturing it). I'd probably modify it to this...

 

function isTime($time) {
   return preg_match("#^(?:[01][0-9]|2[0-3]):[0-5][0-9]$#", $time);
}

But thats just my opinion.

Link to comment
https://forums.phpfreaks.com/topic/182647-validating-time/#findComment-963986
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.