
Hi. I have a text box in html to get time in the format of hour:minutes. I have to validate that the given information in that text box is in the correct format or not using java script. Please help me for doing project.

Here is a code that accepts the time in the format hh:mm. If the user enters the time in wrong format, it displays the error message.
<html>
<script language="JavaScript">
function check(timeStr) {
var regex = /^(\d{1,2}):(\d{2})?$/;
var timArr = timeStr.match(regex);
if (timArr == null) {
alert("Time is not in a valid format.");
document.frm.timetext.value="";
return false;
}
hour = timArr[1];
minute = timArr[2];
if (hour < 0 || hour > 23) {
alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
document.frm.timetext.value="";
return false;
}
if (minute<0 || minute > 59) {
alert ("Minute must be between 0 and 59.");
document.frm.timetext.value="";
return false;
}
return false;
}
</script>
<form name="frm">
Time (HH:MM)
<input type="text" name="timetext">
<input type="button" value="Check" onclick="return check(document.frm.timetext.value);">
</form>
</html>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.