2
votes

I have a input datetime format: dd/mm/yy hh:mm
I use jquery validate to check user' input & it work well.
But now, I want validate hh:mm must not in a definite range, example: I have blocking time from 07:00 to 15:00, so hh:mm user input must not in this range.

How to validate this using javascript (jquery validate, ...)?

1

1 Answers

5
votes

Using plain Javascript:

<script>
    function validate_time(t,st,et){
       t = t.split(/:/);
       st = st.split(/:/);
       et = et.split(/:/);
       return (t[0] < st[0] 
            || t[0] > et[0] 
            || (t[0] == st[0] && t[1] < st[1])
            || (t[0] == et[0] && t[1] > et[1]));
    }

    // Call like:
    validate_time("08:00","07:00","15:00");
</script>