views:

99

answers:

4

Hi guys,

Basically, I'd like to have an input that when blur'd, will check the input to make sure it's in the format...

[24hour] : [minutes]

So for example 13:00, or 15:30.

So I guess I have to split it up into three parts, check the first bit is between 0 and 24, then check it has the semi-colon, then check it has a number between 0 and 60.

Going more complicated than that, it'd be fantastic to have it so if the user enters 19 it'll complete it as 19:00 for example.

I am using jQuery fwiw, but regular code is fine, for example I'm using this little piece of code so far which works fine, to convert . inputs to :

tempval = $(this).val().replace(".", ":");
$(this).val(tempval);

Not sure where to start with this, if anyone could recommend me some reading that'd be fantastic, thank you!

+4  A: 

([0-1 ]?[0-9]|2[0-3]):([0-5][0-9])

I think that's the regex you're looking for (not specifically for javascript though).

http://www.regular-expressions.info/ This site has an excellent amount of info for language-specific regular expressions! Cheers!

Zachery Delafosse
Ahh, thank you - I'll take a look at this now!
Nick
Thanks, just tried implementing it like so...if($(this+":regex('([0-1 ]?[0-9]|2[0-3]):([0-5][0-9])')")) {alert('is fine');} ... for some reason it always displays 'it works!' even when it shouldn't :(
Nick
Try something like this...if(this.match(/^([0-1 ]?[0-9]|2[0-3]):([0-5][0-9])$/)) {alert('is fine');}
Zachery Delafosse
Ahh, thank you very much Natso - hopefully I'll start using regex now that I know how to implement them, cheers :)
Nick
+1  A: 

There are a bunch of widgets that already deal with time validation - try googling for "jQuery time widget" - the first result doesn't look bad.

Dominic Rodger
Many thanks - this looks great, but unfortunately I'd rather not use a widget if possible as the users won't be very tech savy and I think simply entering the text (and validated/edited accordingly using Javascript) could be better than using some sort of impressive yet slightly confusing picker.Also worth noting they have to select 14 different times, so the speed in which they can do it is quite important.Cheers
Nick
+1  A: 
var re = /^(\d+)(:\d+)?$/;
var match = re.match(yourstring);

Now if the match has succeeded match is an array with the matched pieces: match[0] is the whole of yourstring (you don't care about that), match[1] has the digits before the colon (if any colon, else just digits), match[2] if it exists has the colon followed by the digits after it. So now you just need to perform your numeric tests on match[1], and possibly match[2] minus the leading colon, to ensure the numbers are correct.

Alex Martelli
Many thanks alex, for some reason I didn't think to split it up like this, but it's a great solution that I'll look into now!
Nick
+1  A: 

I suggest using masked input That way the wrong input will be prevented in the first place.

Disclaimer: I haven't used that plugin myself, just found it by keywords "masked input"

maksymko
This could be absolutely perfect - thank you!I'd love to do it myself if possible, and may revisit the code when I have some more time, but I think this could be ideal to use for now! Thank you :)
Nick