-3

I want to check an input in a form to validate this format.

I use this RegEx but it doesn't work like i want ( to be in this format HH:MM:SS:mm )

HH : hours
MM : minutes
SS : seconds
mm : milliseconds

/^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\\.([0-9]{1,3}))?$/i

I have used the jquery validation plugin for testing the form, this is the example of method that i create including this RegEx :

$.validator.addMethod("timeFormat", function(value, element) {  
                return this.optional(element) || /^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\\.([0-9]{1,3}))?$/i.test(value);  
            }, "");

what's wrong with this ?

Praveen
  • 55,303
  • 33
  • 133
  • 164
kach
  • 799
  • 3
  • 13
  • 23
  • This is an extremely simple regex. The fact that you ask us to write it for you shows that you are too lazy to read even the most simple regex introduction tutorial and do not show the "minimal understanding" required to ask us for code. – ThiefMaster Oct 12 '13 at 11:01
  • No, it's just that i can't figure out how to make it right, i had tested this /^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\\.([0-9]{1,3}))?$/i but it doesn't work like i want :( – kach Oct 12 '13 at 11:04
  • 2
    See, now include that regex in your question and tell us why it didn't work. Then we can re-open it. – ThiefMaster Oct 12 '13 at 11:05
  • Also show us some input that should work but didn't – ThiefMaster Oct 12 '13 at 11:10
  • Try this regex `\d{1,2}:\d{1,2}:\d{1,2}:\d{1,2}` – Praveen Oct 12 '13 at 11:11
  • 1
    @user1671639 Is `99:99:99:99` a valid time? The OP should just replace `?:\\.([0-9]{1,3})` with `:\d{2}`. – Rob W Oct 12 '13 at 11:11
  • @RobW Agreed. just wanted to show a quick try – Praveen Oct 12 '13 at 11:12
  • closely related to [Regex pattern for HH:MM:SS time string](http://stackoverflow.com/questions/8318236/regex-pattern-for-hhmmss-time-string) – Praveen Oct 12 '13 at 11:15

2 Answers2

2

Try this regex

([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(:|\.)\d{2}

Regexplained

Praveen
  • 55,303
  • 33
  • 133
  • 164
  • that's not working in last 2 digits of milliseconds ?? i have tested this and it's work but in milliseconds it can be 99 maximun : ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]):([0-5][0-9]) – kach Oct 12 '13 at 11:32
  • @kach can you try this `([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(:|\.)([0-9]{1,2})` – Praveen Oct 12 '13 at 11:34
  • @kach I too checked this `([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(:|\.)\d{2}` this seems fine – Praveen Oct 12 '13 at 11:42
1

I think this is what you need:

([01]\d|2[0-3])(:[0-5]\d){2}:\d{1,3} 

(Milliseconds may be up to three digits)

nima
  • 6,566
  • 4
  • 45
  • 57