Click to See Complete Forum and Search --> : Time Validations?
0kc0mputer
07-24-2003, 09:50 AM
Hi, I'm new to javascript, and I need to find a way to validate a Time field. When a user updates a record, the time comes in HH:MM:SS AM or HH:MM:SS PM. However, it does not matter what format they enter when updating the record. It can be military or HH:MM AM.
Is there a validation function out there that will allow me to check for all valid times? If not, the first mentioned format will do.
Any help would be appreciated.
Charles
07-24-2003, 09:56 AM
If you want time and date then just use the Date constructor:
<input type="text" onchange="this.value=new Date(this.value).toDateString()">
But otherwise you will have to prepend the date:
<input type="text" onchange="this.value=new Date(new Date().toDateString() + this.value).toDateString()">
0kc0mputer
07-24-2003, 10:06 AM
No date, just time. And by validate, I mean checking to see if it is a valid time before submitting the form. I.E, if a user has entered "11:65 PM", an alert message will say "What are you thinking? That's not a valid time."
Charles
07-24-2003, 01:56 PM
Give this a try:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<form action="" onsubmit="return false">
<style type="text/css">
<!--
label {display:block; margin:1ex 0ex}
-->
</style>
<div>
<label>Time<br><input type="text" onchange="this.value = new Date([new Date().toDateString(), this.value].join(' ')).toTimeString()"></label>
<button>Submit</button>
</div>
</form>