Skip to content

Commit 7360d30

Browse files
committed
time validation
1 parent d428ef1 commit 7360d30

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

index.html

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ <h3>Date Picker:</h3>
962962
</div>
963963
<h3>Time Picker:</h3>
964964
<div>
965-
<input type="text" id="time-picker" size="12" readonly="readonly"/>
965+
<input type="text" id="time-picker" size="12" maxlength="5"/>
966966
</div>
967967
</div>
968968
<div class="f-item">
@@ -972,7 +972,7 @@ <h3>Time Picker:</h3>
972972
Date Picker:
973973
&lt;input type=&quot;text&quot; id=&quot;date-picker&quot; size=&quot;12&quot; readonly=&quot;readonly&quot;/&gt;
974974
Time Picker:
975-
&lt;input type=&quot;text&quot; id=&quot;time-picker&quot; size=&quot;12&quot; readonly=&quot;readonly&quot;/&gt;
975+
&lt;input type=&quot;text&quot; id=&quot;time-picker&quot; size=&quot;12&quot; maxLength=&quot;5&quot;/&gt;
976976
&lt;script type=&quot;text/javascript&quot;&gt;
977977
var date = new Date();
978978
$('#date-picker').datepicker({
@@ -981,14 +981,22 @@ <h3>Time Picker:</h3>
981981
changeYear: true,
982982
minDate: date,
983983
});
984-
$(&quot;#time-picker&quot;).on('click', function() {
985-
date = new Date();
986-
$(this).timepicker('destroy').timepicker({
987-
minTime: {
988-
hour: date.getHours(),
989-
minute: date.getMinutes()
984+
$(&quot;#time-picker&quot;)
985+
.on('click', function() {
986+
date = new Date();
987+
$(this).timepicker('destroy').timepicker({
988+
minTime: {
989+
hour: date.getHours(),
990+
minute: date.getMinutes()
991+
}
992+
});
993+
})
994+
.on('keypress', debounce(function(time) {
995+
var regex = new RegExp(time);
996+
if (!/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/.test(regex)) {
997+
$('#time-picker').parent().append($('<p>', {class: 'error-msg', text: 'Time is invalid'}));
990998
}
991-
});
999+
}));
9921000
});
9931001
&lt;/script&gt;
9941002
</code>

style.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,20 @@ $(document).ready(function() {
6363
minDate: new Date(),
6464
});
6565
attachTimePicker();
66-
$("#time-picker").on('click', function() {
67-
$(this).timepicker('destroy');
68-
attachTimePicker();
69-
});
66+
$("#time-picker")
67+
.on('click', function() {
68+
$(this).timepicker('destroy');
69+
attachTimePicker();
70+
})
71+
.on('keypress', debounce(function(e) {
72+
if (!/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/.test(e.target.value)) {
73+
if ($('#invalid-time').length === 0) {
74+
$('#time-picker').parent().append($('<p>', {class: 'error-msg', id: 'invalid-time', text: 'Invalid time'}));
75+
}
76+
} else {
77+
$('#invalid-time').remove();
78+
}
79+
}));
7080

7181
function attachTimePicker() {
7282
var date = new Date();
@@ -77,4 +87,21 @@ $(document).ready(function() {
7787
}
7888
});
7989
}
90+
91+
function debounce(func, wait, immediate) {
92+
'use strict';
93+
var timeout;
94+
var delay = wait || 500;
95+
return function() {
96+
var context = this, args = arguments;
97+
var later = function() {
98+
timeout = null;
99+
if (!immediate) func.apply(context, args);
100+
};
101+
var callNow = immediate && !timeout;
102+
clearTimeout(timeout);
103+
timeout = setTimeout(later, delay);
104+
if (callNow) func.apply(context, args);
105+
};
106+
}
80107
});

0 commit comments

Comments
 (0)