View Full Version : Date/Time Validation in JavaScript
Leinad
03-25-2003, 04:41 AM
Hi JavaScript Guru,
Is it possible to have a Time validation that checks:
12:30 - format ok
1230 - format ok
12 - format not ok
also, one for date:
01/01/03 format ok
010103 format ok
113 format not ok
I wish someone could help me...
Philip M
03-25-2003, 04:04 PM
You have not really explained what you want to do.
Time format - is this 12 or 24-hour clock?
Do you want 3:30 or 03:30? 15:30 or 1530?
Likewise date format - is 1/1/03 acceptable? or 1/1/3?
I suggest that you simplify the position by requiring
your users to enter date/time in a format that you specify,
then validate that format and reject any other.
Here is a script to validate the time in XX:XX format:
function checktime(thetime) {
var a,b,c,f,err=0;
a=thetime.value;
if (a.length != 5) err=1;
b = a.substring(0, 2);
c = a.substring(2, 3);
f = a.substring(3, 5);
if (/\D/g.test(b)) err=1; //not a number
if (/\D/g.test(f)) err=1;
if (b<0 || b>23) err=1;
if (f<0 || f>59) err=1;
if (c != ':') err=1;
if (err==1) {
alert ('That is not a valid time.\nPlease re-enter in format XX:XX ');
thetime.value = "";
thetime.focus();
}
}
You can make a similar script for the date validation.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.