ShaolinF Posted November 23, 2009 Share Posted November 23, 2009 Hi Guys what do you think of the following approach to validate a 24hour time: http://snipplr.com/view/23007/validate-time/ Are there any better approaches ? Link to comment https://forums.phpfreaks.com/topic/182647-validating-time/ Share on other sites More sharing options...
cags Posted November 23, 2009 Share Posted November 23, 2009 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 More sharing options...
ShaolinF Posted November 23, 2009 Author Share Posted November 23, 2009 Thanks cags. BTW, do you know what the # expression does ? Link to comment https://forums.phpfreaks.com/topic/182647-validating-time/#findComment-964022 Share on other sites More sharing options...
cags Posted November 23, 2009 Share Posted November 23, 2009 It doesn't do anything in itself. The # in this case is the character that is used as a delimiter (see manual). Link to comment https://forums.phpfreaks.com/topic/182647-validating-time/#findComment-964027 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.